~ubuntu-branches/ubuntu/maverick/clamav/maverick-proposed

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/lib/VMCore/Instructions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-12-17 12:30:20 UTC
  • mfrom: (0.35.17 sid)
  • Revision ID: james.westby@ubuntu.com-20101217123020-dbtsp3upnaxsgg89
Tags: 0.96.5+dfsg-1ubuntu1.10.10.1
* Microversion update for Maverick (LP: #691414)
  - Improved database login times
  - Expanded use of new bytecode signatures
  - Other bugfixes/improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
//                            CallSite Class
31
31
//===----------------------------------------------------------------------===//
32
32
 
33
 
#define CALLSITE_DELEGATE_GETTER(METHOD) \
34
 
  Instruction *II(getInstruction());     \
35
 
  return isCall()                        \
36
 
    ? cast<CallInst>(II)->METHOD         \
37
 
    : cast<InvokeInst>(II)->METHOD
38
 
 
39
 
#define CALLSITE_DELEGATE_SETTER(METHOD) \
40
 
  Instruction *II(getInstruction());     \
41
 
  if (isCall())                          \
42
 
    cast<CallInst>(II)->METHOD;          \
43
 
  else                                   \
44
 
    cast<InvokeInst>(II)->METHOD
45
 
 
46
 
CallSite::CallSite(Instruction *C) {
47
 
  assert((isa<CallInst>(C) || isa<InvokeInst>(C)) && "Not a call!");
48
 
  I.setPointer(C);
49
 
  I.setInt(isa<CallInst>(C));
50
 
}
51
 
CallingConv::ID CallSite::getCallingConv() const {
52
 
  CALLSITE_DELEGATE_GETTER(getCallingConv());
53
 
}
54
 
void CallSite::setCallingConv(CallingConv::ID CC) {
55
 
  CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
56
 
}
57
 
const AttrListPtr &CallSite::getAttributes() const {
58
 
  CALLSITE_DELEGATE_GETTER(getAttributes());
59
 
}
60
 
void CallSite::setAttributes(const AttrListPtr &PAL) {
61
 
  CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
62
 
}
63
 
bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const {
64
 
  CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
65
 
}
66
 
uint16_t CallSite::getParamAlignment(uint16_t i) const {
67
 
  CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
68
 
}
69
 
bool CallSite::doesNotAccessMemory() const {
70
 
  CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
71
 
}
72
 
void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) {
73
 
  CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
74
 
}
75
 
bool CallSite::onlyReadsMemory() const {
76
 
  CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
77
 
}
78
 
void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) {
79
 
  CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
80
 
}
81
 
bool CallSite::doesNotReturn() const {
82
 
 CALLSITE_DELEGATE_GETTER(doesNotReturn());
83
 
}
84
 
void CallSite::setDoesNotReturn(bool doesNotReturn) {
85
 
  CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
86
 
}
87
 
bool CallSite::doesNotThrow() const {
88
 
  CALLSITE_DELEGATE_GETTER(doesNotThrow());
89
 
}
90
 
void CallSite::setDoesNotThrow(bool doesNotThrow) {
91
 
  CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
92
 
}
93
 
 
94
 
bool CallSite::hasArgument(const Value *Arg) const {
95
 
  for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI)
96
 
    if (AI->get() == Arg)
97
 
      return true;
98
 
  return false;
99
 
}
100
 
 
101
 
#undef CALLSITE_DELEGATE_GETTER
102
 
#undef CALLSITE_DELEGATE_SETTER
 
33
User::op_iterator CallSite::getCallee() const {
 
34
  Instruction *II(getInstruction());
 
35
  return isCall()
 
36
    ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
 
37
    : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
 
38
}
103
39
 
104
40
//===----------------------------------------------------------------------===//
105
41
//                            TerminatorInst Class
295
231
 
296
232
void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
297
233
  assert(NumOperands == NumParams+1 && "NumOperands not set up?");
298
 
  Use *OL = OperandList;
299
 
  OL[0] = Func;
 
234
  Op<-1>() = Func;
300
235
 
301
236
  const FunctionType *FTy =
302
237
    cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
309
244
    assert((i >= FTy->getNumParams() || 
310
245
            FTy->getParamType(i) == Params[i]->getType()) &&
311
246
           "Calling a function with a bad signature!");
312
 
    OL[i+1] = Params[i];
 
247
    OperandList[i] = Params[i];
313
248
  }
314
249
}
315
250
 
316
251
void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
317
252
  assert(NumOperands == 3 && "NumOperands not set up?");
318
 
  Use *OL = OperandList;
319
 
  OL[0] = Func;
320
 
  OL[1] = Actual1;
321
 
  OL[2] = Actual2;
 
253
  Op<-1>() = Func;
 
254
  Op<0>() = Actual1;
 
255
  Op<1>() = Actual2;
322
256
 
323
257
  const FunctionType *FTy =
324
258
    cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
337
271
 
338
272
void CallInst::init(Value *Func, Value *Actual) {
339
273
  assert(NumOperands == 2 && "NumOperands not set up?");
340
 
  Use *OL = OperandList;
341
 
  OL[0] = Func;
342
 
  OL[1] = Actual;
 
274
  Op<-1>() = Func;
 
275
  Op<0>() = Actual;
343
276
 
344
277
  const FunctionType *FTy =
345
278
    cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
355
288
 
356
289
void CallInst::init(Value *Func) {
357
290
  assert(NumOperands == 1 && "NumOperands not set up?");
358
 
  Use *OL = OperandList;
359
 
  OL[0] = Func;
 
291
  Op<-1>() = Func;
360
292
 
361
293
  const FunctionType *FTy =
362
294
    cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
537
469
Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
538
470
                                    const Type *IntPtrTy, const Type *AllocTy,
539
471
                                    Value *AllocSize, Value *ArraySize,
 
472
                                    Function * MallocF,
540
473
                                    const Twine &Name) {
541
474
  return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
542
 
                      ArraySize, NULL, Name);
 
475
                      ArraySize, MallocF, Name);
543
476
}
544
477
 
545
478
/// CreateMalloc - Generate the IR for a call to malloc:
591
524
}
592
525
 
593
526
/// CreateFree - Generate the IR for a call to the builtin free function.
594
 
void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
595
 
  createFree(Source, InsertBefore, NULL);
 
527
Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
 
528
  return createFree(Source, InsertBefore, NULL);
596
529
}
597
530
 
598
531
/// CreateFree - Generate the IR for a call to the builtin free function.
611
544
void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
612
545
                      Value* const *Args, unsigned NumArgs) {
613
546
  assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
614
 
  Use *OL = OperandList;
615
 
  OL[0] = Fn;
616
 
  OL[1] = IfNormal;
617
 
  OL[2] = IfException;
 
547
  Op<-3>() = Fn;
 
548
  Op<-2>() = IfNormal;
 
549
  Op<-1>() = IfException;
618
550
  const FunctionType *FTy =
619
551
    cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
620
552
  FTy = FTy;  // silence warning.
621
553
 
622
554
  assert(((NumArgs == FTy->getNumParams()) ||
623
555
          (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
624
 
         "Calling a function with bad signature");
 
556
         "Invoking a function with bad signature");
625
557
 
 
558
  Use *OL = OperandList;
626
559
  for (unsigned i = 0, e = NumArgs; i != e; i++) {
627
560
    assert((i >= FTy->getNumParams() || 
628
561
            FTy->getParamType(i) == Args[i]->getType()) &&
629
562
           "Invoking a function with a bad signature!");
630
563
    
631
 
    OL[i+3] = Args[i];
 
564
    OL[i] = Args[i];
632
565
  }
633
566
}
634
567
 
892
825
  else {
893
826
    assert(!isa<BasicBlock>(Amt) &&
894
827
           "Passed basic block into allocation size parameter! Use other ctor");
895
 
    assert(Amt->getType()->isIntegerTy(32) &&
896
 
           "Allocation array size is not a 32-bit integer!");
 
828
    assert(Amt->getType()->isIntegerTy() &&
 
829
           "Allocation array size is not an integer!");
897
830
  }
898
831
  return Amt;
899
832
}
958
891
 
959
892
void AllocaInst::setAlignment(unsigned Align) {
960
893
  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
 
894
  assert(Align <= MaximumAlignment &&
 
895
         "Alignment is greater than MaximumAlignment!");
961
896
  setInstructionSubclassData(Log2_32(Align) + 1);
962
897
  assert(getAlignment() == Align && "Alignment representation error!");
963
898
}
1093
1028
 
1094
1029
void LoadInst::setAlignment(unsigned Align) {
1095
1030
  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
 
1031
  assert(Align <= MaximumAlignment &&
 
1032
         "Alignment is greater than MaximumAlignment!");
1096
1033
  setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1097
1034
                             ((Log2_32(Align)+1)<<1));
 
1035
  assert(getAlignment() == Align && "Alignment representation error!");
1098
1036
}
1099
1037
 
1100
1038
//===----------------------------------------------------------------------===//
1189
1127
 
1190
1128
void StoreInst::setAlignment(unsigned Align) {
1191
1129
  assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
 
1130
  assert(Align <= MaximumAlignment &&
 
1131
         "Alignment is greater than MaximumAlignment!");
1192
1132
  setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1193
1133
                             ((Log2_32(Align)+1) << 1));
 
1134
  assert(getAlignment() == Align && "Alignment representation error!");
1194
1135
}
1195
1136
 
1196
1137
//===----------------------------------------------------------------------===//
1489
1430
    return false;
1490
1431
  
1491
1432
  const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1492
 
  if (!isa<Constant>(Mask) || MaskTy == 0 ||
1493
 
      !MaskTy->getElementType()->isIntegerTy(32))
1494
 
    return false;
 
1433
  if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
 
1434
    return false;
 
1435
 
 
1436
  // Check to see if Mask is valid.
 
1437
  if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
 
1438
    const VectorType *VTy = cast<VectorType>(V1->getType());
 
1439
    for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
 
1440
      if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
 
1441
        if (CI->uge(VTy->getNumElements()*2))
 
1442
          return false;
 
1443
      } else if (!isa<UndefValue>(MV->getOperand(i))) {
 
1444
        return false;
 
1445
      }
 
1446
    }
 
1447
  }
 
1448
  else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
 
1449
    return false;
 
1450
  
1495
1451
  return true;
1496
1452
}
1497
1453
 
1520
1476
  Op<0>() = Agg;
1521
1477
  Op<1>() = Val;
1522
1478
 
1523
 
  Indices.insert(Indices.end(), Idx, Idx + NumIdx);
 
1479
  Indices.append(Idx, Idx + NumIdx);
1524
1480
  setName(Name);
1525
1481
}
1526
1482
 
1573
1529
                            const Twine &Name) {
1574
1530
  assert(NumOperands == 1 && "NumOperands not initialized?");
1575
1531
 
1576
 
  Indices.insert(Indices.end(), Idx, Idx + NumIdx);
 
1532
  Indices.append(Idx, Idx + NumIdx);
1577
1533
  setName(Name);
1578
1534
}
1579
1535
 
1626
1582
//                             BinaryOperator Class
1627
1583
//===----------------------------------------------------------------------===//
1628
1584
 
1629
 
/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1630
 
/// type is floating-point, to help provide compatibility with an older API.
1631
 
///
1632
 
static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1633
 
                                             const Type *Ty) {
1634
 
  // API compatibility: Adjust integer opcodes to floating-point opcodes.
1635
 
  if (Ty->isFPOrFPVectorTy()) {
1636
 
    if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1637
 
    else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1638
 
    else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1639
 
  }
1640
 
  return iType;
1641
 
}
1642
 
 
1643
1585
BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1644
1586
                               const Type *Ty, const Twine &Name,
1645
1587
                               Instruction *InsertBefore)
1646
 
  : Instruction(Ty, AdjustIType(iType, Ty),
 
1588
  : Instruction(Ty, iType,
1647
1589
                OperandTraits<BinaryOperator>::op_begin(this),
1648
1590
                OperandTraits<BinaryOperator>::operands(this),
1649
1591
                InsertBefore) {
1650
1592
  Op<0>() = S1;
1651
1593
  Op<1>() = S2;
1652
 
  init(AdjustIType(iType, Ty));
 
1594
  init(iType);
1653
1595
  setName(Name);
1654
1596
}
1655
1597
 
1656
1598
BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, 
1657
1599
                               const Type *Ty, const Twine &Name,
1658
1600
                               BasicBlock *InsertAtEnd)
1659
 
  : Instruction(Ty, AdjustIType(iType, Ty),
 
1601
  : Instruction(Ty, iType,
1660
1602
                OperandTraits<BinaryOperator>::op_begin(this),
1661
1603
                OperandTraits<BinaryOperator>::operands(this),
1662
1604
                InsertAtEnd) {
1663
1605
  Op<0>() = S1;
1664
1606
  Op<1>() = S2;
1665
 
  init(AdjustIType(iType, Ty));
 
1607
  init(iType);
1666
1608
  setName(Name);
1667
1609
}
1668
1610
 
1989
1931
/// # bitcast i32* %x to i8*
1990
1932
/// # bitcast <2 x i32> %x to <4 x i16> 
1991
1933
/// # ptrtoint i32* %x to i32     ; on 32-bit plaforms only
1992
 
/// @brief Determine if a cast is a no-op.
1993
 
bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1994
 
  switch (getOpcode()) {
 
1934
/// @brief Determine if the described cast is a no-op.
 
1935
bool CastInst::isNoopCast(Instruction::CastOps Opcode,
 
1936
                          const Type *SrcTy,
 
1937
                          const Type *DestTy,
 
1938
                          const Type *IntPtrTy) {
 
1939
  switch (Opcode) {
1995
1940
    default:
1996
1941
      assert(!"Invalid CastOp");
1997
1942
    case Instruction::Trunc:
2008
1953
      return true;  // BitCast never modifies bits.
2009
1954
    case Instruction::PtrToInt:
2010
1955
      return IntPtrTy->getScalarSizeInBits() ==
2011
 
             getType()->getScalarSizeInBits();
 
1956
             DestTy->getScalarSizeInBits();
2012
1957
    case Instruction::IntToPtr:
2013
1958
      return IntPtrTy->getScalarSizeInBits() ==
2014
 
             getOperand(0)->getType()->getScalarSizeInBits();
 
1959
             SrcTy->getScalarSizeInBits();
2015
1960
  }
2016
1961
}
2017
1962
 
 
1963
/// @brief Determine if a cast is a no-op.
 
1964
bool CastInst::isNoopCast(const Type *IntPtrTy) const {
 
1965
  return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
 
1966
}
 
1967
 
2018
1968
/// This function determines if a pair of casts can be eliminated and what 
2019
1969
/// opcode should be used in the elimination. This assumes that there are two 
2020
1970
/// instructions like this:
2047
1997
  // FPEXT         <       FloatPt      n/a        FloatPt      n/a   
2048
1998
  // PTRTOINT     n/a      Pointer      n/a        Integral   Unsigned
2049
1999
  // INTTOPTR     n/a      Integral   Unsigned     Pointer      n/a
2050
 
  // BITCONVERT    =       FirstClass   n/a       FirstClass    n/a   
 
2000
  // BITCAST       =       FirstClass   n/a       FirstClass    n/a   
2051
2001
  //
2052
2002
  // NOTE: some transforms are safe, but we consider them to be non-profitable.
2053
2003
  // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2077
2027
    { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr    |
2078
2028
    {  5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast    -+
2079
2029
  };
 
2030
  
 
2031
  // If either of the casts are a bitcast from scalar to vector, disallow the
 
2032
  // merging.
 
2033
  if ((firstOp == Instruction::BitCast &&
 
2034
       isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
 
2035
      (secondOp == Instruction::BitCast &&
 
2036
       isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
 
2037
    return 0; // Disallowed
2080
2038
 
2081
2039
  int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2082
2040
                            [secondOp-Instruction::CastOpsBegin];