Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Dan Gohman | d2a251f | 2009-07-17 21:33:58 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ConstantRange.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // CallSite Class |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 32 | User::op_iterator CallSite::getCallee() const { |
| 33 | Instruction *II(getInstruction()); |
| 34 | return isCall() |
Gabor Greif | 638c823 | 2010-08-05 21:25:49 +0000 | [diff] [blame] | 35 | ? cast<CallInst>(II)->op_end() - 1 // Skip Callee |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 36 | : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // TerminatorInst Class |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | // Out of line virtual method, so the vtable, etc has a home. |
| 44 | TerminatorInst::~TerminatorInst() { |
| 45 | } |
| 46 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // UnaryInstruction Class |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 51 | // Out of line virtual method, so the vtable, etc has a home. |
| 52 | UnaryInstruction::~UnaryInstruction() { |
| 53 | } |
| 54 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 55 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 56 | // SelectInst Class |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | |
| 59 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 60 | /// for a select operation, otherwise return null. |
| 61 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 62 | if (Op1->getType() != Op2->getType()) |
| 63 | return "both values to select must have same type"; |
| 64 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 65 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 66 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 67 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 68 | return "vector select condition element type must be i1"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 69 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 70 | if (ET == 0) |
| 71 | return "selected values for vector select must be vectors"; |
| 72 | if (ET->getNumElements() != VT->getNumElements()) |
| 73 | return "vector select requires selected vectors to have " |
| 74 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 75 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 76 | return "select condition must be i1 or <n x i1>"; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 83 | // PHINode Class |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | PHINode::PHINode(const PHINode &PN) |
| 87 | : Instruction(PN.getType(), Instruction::PHI, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 88 | allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 89 | ReservedSpace(PN.getNumOperands()) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 90 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 91 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 92 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 95 | PHINode::~PHINode() { |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 96 | dropHungoffUses(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 99 | Use *PHINode::allocHungoffUses(unsigned N) const { |
| 100 | // Allocate the array of Uses of the incoming values, followed by a pointer |
| 101 | // (with bottom bit set) to the User, followed by the array of pointers to |
| 102 | // the incoming basic blocks. |
| 103 | size_t size = N * sizeof(Use) + sizeof(Use::UserRef) |
| 104 | + N * sizeof(BasicBlock*); |
| 105 | Use *Begin = static_cast<Use*>(::operator new(size)); |
| 106 | Use *End = Begin + N; |
| 107 | (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1); |
| 108 | return Use::initTags(Begin, End); |
| 109 | } |
| 110 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 111 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 112 | // predecessor basic block is deleted. |
| 113 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 114 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 115 | |
| 116 | // Move everything after this operand down. |
| 117 | // |
| 118 | // FIXME: we could just swap with the end of the list, then erase. However, |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 119 | // clients might not expect this to happen. The code as it is thrashes the |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 120 | // use/def lists, which is kinda lame. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 121 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 122 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 123 | |
| 124 | // Nuke the last value. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 125 | Op<-1>().set(0); |
| 126 | --NumOperands; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 127 | |
| 128 | // If the PHI node is dead, because it has zero entries, nuke it now. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 129 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 130 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 131 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 132 | eraseFromParent(); |
| 133 | } |
| 134 | return Removed; |
| 135 | } |
| 136 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 137 | /// growOperands - grow operands - This grows the operand list in response |
| 138 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 139 | /// times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 140 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 141 | void PHINode::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 142 | unsigned e = getNumOperands(); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 143 | unsigned NumOps = e + e / 2; |
| 144 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 145 | |
| 146 | Use *OldOps = op_begin(); |
| 147 | BasicBlock **OldBlocks = block_begin(); |
Jay Foad | e03c05c | 2011-06-20 14:38:01 +0000 | [diff] [blame] | 148 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 149 | ReservedSpace = NumOps; |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 150 | OperandList = allocHungoffUses(ReservedSpace); |
| 151 | |
| 152 | std::copy(OldOps, OldOps + e, op_begin()); |
| 153 | std::copy(OldBlocks, OldBlocks + e, block_begin()); |
| 154 | |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 155 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 158 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 159 | /// value, return the value, otherwise return null. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 160 | Value *PHINode::hasConstantValue() const { |
| 161 | // Exploit the fact that phi nodes always have at least one entry. |
| 162 | Value *ConstantValue = getIncomingValue(0); |
| 163 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
| 164 | if (getIncomingValue(i) != ConstantValue) |
| 165 | return 0; // Incoming values not all the same. |
| 166 | return ConstantValue; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 169 | |
| 170 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 171 | // CallInst Implementation |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 174 | CallInst::~CallInst() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 177 | void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) { |
| 178 | assert(NumOperands == Args.size() + 1 && "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 179 | Op<-1>() = Func; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 180 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 181 | #ifndef NDEBUG |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 182 | FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 183 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 184 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 185 | assert((Args.size() == FTy->getNumParams() || |
| 186 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 187 | "Calling a function with bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 188 | |
| 189 | for (unsigned i = 0; i != Args.size(); ++i) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 190 | assert((i >= FTy->getNumParams() || |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 191 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 192 | "Calling a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 193 | #endif |
| 194 | |
| 195 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 196 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 199 | void CallInst::init(Value *Func, const Twine &NameStr) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 200 | assert(NumOperands == 1 && "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 201 | Op<-1>() = Func; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 202 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 203 | #ifndef NDEBUG |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 204 | FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 205 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 206 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 207 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 208 | #endif |
| 209 | |
| 210 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 213 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 214 | Instruction *InsertBefore) |
| 215 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 216 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 217 | Instruction::Call, |
| 218 | OperandTraits<CallInst>::op_end(this) - 1, |
| 219 | 1, InsertBefore) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 220 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 223 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 224 | BasicBlock *InsertAtEnd) |
| 225 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 226 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 227 | Instruction::Call, |
| 228 | OperandTraits<CallInst>::op_end(this) - 1, |
| 229 | 1, InsertAtEnd) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 230 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 233 | CallInst::CallInst(const CallInst &CI) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 234 | : Instruction(CI.getType(), Instruction::Call, |
| 235 | OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 236 | CI.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 237 | setAttributes(CI.getAttributes()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 238 | setTailCall(CI.isTailCall()); |
| 239 | setCallingConv(CI.getCallingConv()); |
| 240 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 241 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 242 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 245 | void CallInst::addAttribute(unsigned i, Attributes attr) { |
| 246 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 247 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 248 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 251 | void CallInst::removeAttribute(unsigned i, Attributes attr) { |
| 252 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 253 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 254 | setAttributes(PAL); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 257 | bool CallInst::paramHasAttr(unsigned i, Attributes attr) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 258 | if (AttributeList.paramHasAttr(i, attr)) |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 259 | return true; |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 260 | if (const Function *F = getCalledFunction()) |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 261 | return F->paramHasAttr(i, attr); |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 262 | return false; |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 265 | /// IsConstantOne - Return true only if val is constant int 1 |
| 266 | static bool IsConstantOne(Value *val) { |
| 267 | assert(val && "IsConstantOne does not work with NULL val"); |
| 268 | return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); |
| 269 | } |
| 270 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 271 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 272 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
| 273 | Type *AllocTy, Value *AllocSize, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 274 | Value *ArraySize, Function *MallocF, |
| 275 | const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 276 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 277 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 278 | |
| 279 | // malloc(type) becomes: |
| 280 | // bitcast (i8* malloc(typeSize)) to type* |
| 281 | // malloc(type, arraySize) becomes: |
| 282 | // bitcast (i8 *malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 283 | if (!ArraySize) |
| 284 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 285 | else if (ArraySize->getType() != IntPtrTy) { |
| 286 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 287 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 288 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 289 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 290 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 291 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 292 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 293 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 294 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 295 | if (IsConstantOne(AllocSize)) { |
| 296 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 297 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 298 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 299 | false /*ZExt*/); |
| 300 | // Malloc arg is constant product of type size and array size |
| 301 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 302 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 303 | // Multiply type size by the array size... |
| 304 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 305 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 306 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 307 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 308 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 309 | "mallocsize", InsertAtEnd); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 310 | } |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 311 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 312 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 313 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 314 | // Create the call to Malloc. |
| 315 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 316 | Module* M = BB->getParent()->getParent(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 317 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 318 | Value *MallocFunc = MallocF; |
| 319 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 320 | // prototype malloc as "void *malloc(size_t)" |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 321 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 322 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 323 | CallInst *MCall = NULL; |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 324 | Instruction *Result = NULL; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 325 | if (InsertBefore) { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 326 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 327 | Result = MCall; |
| 328 | if (Result->getType() != AllocPtrType) |
| 329 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 330 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 331 | } else { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 332 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 333 | Result = MCall; |
| 334 | if (Result->getType() != AllocPtrType) { |
| 335 | InsertAtEnd->getInstList().push_back(MCall); |
| 336 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 337 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 338 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 339 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 340 | MCall->setTailCall(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 341 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 342 | MCall->setCallingConv(F->getCallingConv()); |
| 343 | if (!F->doesNotAlias(0)) F->setDoesNotAlias(0); |
| 344 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 345 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 346 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 347 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 351 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 352 | /// possibly multiplied by the array size if the array size is not |
| 353 | /// constant 1. |
| 354 | /// 2. Call malloc with that argument. |
| 355 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 356 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 357 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 358 | Value *AllocSize, Value *ArraySize, |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 359 | Function * MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 360 | const Twine &Name) { |
| 361 | return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize, |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 362 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 366 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 367 | /// possibly multiplied by the array size if the array size is not |
| 368 | /// constant 1. |
| 369 | /// 2. Call malloc with that argument. |
| 370 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 371 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 372 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 373 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 374 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 375 | Value *AllocSize, Value *ArraySize, |
| 376 | Function *MallocF, const Twine &Name) { |
| 377 | return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 378 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 379 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 380 | |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 381 | static Instruction* createFree(Value* Source, Instruction *InsertBefore, |
| 382 | BasicBlock *InsertAtEnd) { |
| 383 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 384 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 385 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 386 | "Can not free something of nonpointer type!"); |
| 387 | |
| 388 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 389 | Module* M = BB->getParent()->getParent(); |
| 390 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 391 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 392 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 393 | // prototype free as "void free(void*)" |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 394 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 395 | CallInst* Result = NULL; |
| 396 | Value *PtrCast = Source; |
| 397 | if (InsertBefore) { |
| 398 | if (Source->getType() != IntPtrTy) |
| 399 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
| 400 | Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore); |
| 401 | } else { |
| 402 | if (Source->getType() != IntPtrTy) |
| 403 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
| 404 | Result = CallInst::Create(FreeFunc, PtrCast, ""); |
| 405 | } |
| 406 | Result->setTailCall(); |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 407 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 408 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 409 | |
| 410 | return Result; |
| 411 | } |
| 412 | |
| 413 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 414 | Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) { |
| 415 | return createFree(Source, InsertBefore, NULL); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 419 | /// Note: This function does not add the call to the basic block, that is the |
| 420 | /// responsibility of the caller. |
| 421 | Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) { |
| 422 | Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd); |
| 423 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 424 | return FreeCall; |
| 425 | } |
| 426 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 427 | //===----------------------------------------------------------------------===// |
| 428 | // InvokeInst Implementation |
| 429 | //===----------------------------------------------------------------------===// |
| 430 | |
| 431 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 432 | ArrayRef<Value *> Args, const Twine &NameStr) { |
| 433 | assert(NumOperands == 3 + Args.size() && "NumOperands not set up?"); |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 434 | Op<-3>() = Fn; |
| 435 | Op<-2>() = IfNormal; |
| 436 | Op<-1>() = IfException; |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 437 | |
| 438 | #ifndef NDEBUG |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 439 | FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 440 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 441 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 442 | assert(((Args.size() == FTy->getNumParams()) || |
| 443 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 444 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 445 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 446 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 447 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 448 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 449 | "Invoking a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 450 | #endif |
| 451 | |
| 452 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 453 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 456 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 457 | : TerminatorInst(II.getType(), Instruction::Invoke, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 458 | OperandTraits<InvokeInst>::op_end(this) |
| 459 | - II.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 460 | II.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 461 | setAttributes(II.getAttributes()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 462 | setCallingConv(II.getCallingConv()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 463 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 464 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 467 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 468 | return getSuccessor(idx); |
| 469 | } |
| 470 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 471 | return getNumSuccessors(); |
| 472 | } |
| 473 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 474 | return setSuccessor(idx, B); |
| 475 | } |
| 476 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 477 | bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 478 | if (AttributeList.paramHasAttr(i, attr)) |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 479 | return true; |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 480 | if (const Function *F = getCalledFunction()) |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 481 | return F->paramHasAttr(i, attr); |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 482 | return false; |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 485 | void InvokeInst::addAttribute(unsigned i, Attributes attr) { |
| 486 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 487 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 488 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 491 | void InvokeInst::removeAttribute(unsigned i, Attributes attr) { |
| 492 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 493 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 494 | setAttributes(PAL); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 497 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 498 | //===----------------------------------------------------------------------===// |
| 499 | // ReturnInst Implementation |
| 500 | //===----------------------------------------------------------------------===// |
| 501 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 502 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 503 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 504 | OperandTraits<ReturnInst>::op_end(this) - |
| 505 | RI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 506 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 507 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 508 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 509 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 512 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 513 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 514 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 515 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 516 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 517 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 518 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 519 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 520 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 521 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 522 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 523 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 524 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 525 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 526 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 527 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 528 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 531 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 532 | return getNumSuccessors(); |
| 533 | } |
| 534 | |
Devang Patel | ae682fb | 2008-02-26 17:56:20 +0000 | [diff] [blame] | 535 | /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 536 | /// emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 537 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 538 | llvm_unreachable("ReturnInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 541 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 542 | llvm_unreachable("ReturnInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 546 | ReturnInst::~ReturnInst() { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 548 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 549 | //===----------------------------------------------------------------------===// |
| 550 | // UnwindInst Implementation |
| 551 | //===----------------------------------------------------------------------===// |
| 552 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 553 | UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore) |
| 554 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, |
| 555 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 556 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 557 | UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 558 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, |
| 559 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 563 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 564 | return getNumSuccessors(); |
| 565 | } |
| 566 | |
| 567 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 568 | llvm_unreachable("UnwindInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 571 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 572 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 576 | //===----------------------------------------------------------------------===// |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 577 | // ResumeInst Implementation |
| 578 | //===----------------------------------------------------------------------===// |
| 579 | |
| 580 | ResumeInst::ResumeInst(const ResumeInst &RI) |
| 581 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 582 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
| 583 | Op<0>() = RI.Op<0>(); |
| 584 | } |
| 585 | |
| 586 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
| 587 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 588 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
| 589 | Op<0>() = Exn; |
| 590 | } |
| 591 | |
| 592 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
| 593 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 594 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
| 595 | Op<0>() = Exn; |
| 596 | } |
| 597 | |
| 598 | unsigned ResumeInst::getNumSuccessorsV() const { |
| 599 | return getNumSuccessors(); |
| 600 | } |
| 601 | |
| 602 | void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 603 | llvm_unreachable("ResumeInst has no successors!"); |
| 604 | } |
| 605 | |
| 606 | BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const { |
| 607 | llvm_unreachable("ResumeInst has no successors!"); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 612 | // UnreachableInst Implementation |
| 613 | //===----------------------------------------------------------------------===// |
| 614 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 615 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 616 | Instruction *InsertBefore) |
| 617 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 618 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 619 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 620 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 621 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 622 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 625 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 626 | return getNumSuccessors(); |
| 627 | } |
| 628 | |
| 629 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Bill Wendling | ad088e6 | 2011-07-30 05:42:50 +0000 | [diff] [blame] | 630 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
Bill Wendling | ad088e6 | 2011-07-30 05:42:50 +0000 | [diff] [blame] | 634 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 635 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 639 | // BranchInst Implementation |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 642 | void BranchInst::AssertOK() { |
| 643 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 644 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 645 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 648 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 649 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 650 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 651 | 1, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 652 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 653 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 654 | } |
| 655 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 656 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 657 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 658 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 659 | 3, InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 660 | Op<-1>() = IfTrue; |
| 661 | Op<-2>() = IfFalse; |
| 662 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 663 | #ifndef NDEBUG |
| 664 | AssertOK(); |
| 665 | #endif |
| 666 | } |
| 667 | |
| 668 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 669 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 670 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 671 | 1, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 672 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 673 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 677 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 678 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 679 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 680 | 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 681 | Op<-1>() = IfTrue; |
| 682 | Op<-2>() = IfFalse; |
| 683 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 684 | #ifndef NDEBUG |
| 685 | AssertOK(); |
| 686 | #endif |
| 687 | } |
| 688 | |
| 689 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 690 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 691 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 692 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 693 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 694 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 695 | if (BI.getNumOperands() != 1) { |
| 696 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 697 | Op<-3>() = BI.Op<-3>(); |
| 698 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 699 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 700 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 703 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 704 | return getSuccessor(idx); |
| 705 | } |
| 706 | unsigned BranchInst::getNumSuccessorsV() const { |
| 707 | return getNumSuccessors(); |
| 708 | } |
| 709 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 710 | setSuccessor(idx, B); |
| 711 | } |
| 712 | |
| 713 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 714 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 715 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 716 | //===----------------------------------------------------------------------===// |
| 717 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 718 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 719 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 720 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 721 | else { |
| 722 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 723 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 724 | assert(Amt->getType()->isIntegerTy() && |
| 725 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 726 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 727 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 730 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 731 | const Twine &Name, Instruction *InsertBefore) |
| 732 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 733 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
| 734 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 735 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 736 | setName(Name); |
| 737 | } |
| 738 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 739 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 740 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 741 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 742 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
| 743 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 744 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 745 | setName(Name); |
| 746 | } |
| 747 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 748 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 749 | Instruction *InsertBefore) |
| 750 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 751 | getAISize(Ty->getContext(), 0), InsertBefore) { |
| 752 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 753 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 754 | setName(Name); |
| 755 | } |
| 756 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 757 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 758 | BasicBlock *InsertAtEnd) |
| 759 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 760 | getAISize(Ty->getContext(), 0), InsertAtEnd) { |
| 761 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 762 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 763 | setName(Name); |
| 764 | } |
| 765 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 766 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 767 | const Twine &Name, Instruction *InsertBefore) |
| 768 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 769 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 770 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 771 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 772 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 775 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 776 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 777 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 778 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 779 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 780 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 781 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 784 | // Out of line virtual method, so the vtable, etc has a home. |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 785 | AllocaInst::~AllocaInst() { |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 788 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 789 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 790 | assert(Align <= MaximumAlignment && |
| 791 | "Alignment is greater than MaximumAlignment!"); |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 792 | setInstructionSubclassData(Log2_32(Align) + 1); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 793 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 794 | } |
| 795 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 796 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 797 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | 9a1b859 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 798 | return !CI->isOne(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 799 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 802 | Type *AllocaInst::getAllocatedType() const { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 803 | return getType()->getElementType(); |
| 804 | } |
| 805 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 806 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 807 | /// function and is a constant size. If so, the code generator will fold it |
| 808 | /// into the prolog/epilog code, so it is basically free. |
| 809 | bool AllocaInst::isStaticAlloca() const { |
| 810 | // Must be constant size. |
| 811 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 812 | |
| 813 | // Must be in the entry block. |
| 814 | const BasicBlock *Parent = getParent(); |
| 815 | return Parent == &Parent->getParent()->front(); |
| 816 | } |
| 817 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 818 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 819 | // LoadInst Implementation |
| 820 | //===----------------------------------------------------------------------===// |
| 821 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 822 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 823 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 824 | "Ptr must have pointer type."); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 825 | assert(!(isAtomic() && getAlignment() == 0) && |
| 826 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 829 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 830 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 831 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 832 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 833 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 834 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 835 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 836 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 839 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 840 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 841 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 842 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 843 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 844 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 845 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 846 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 849 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 850 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 851 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 852 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 853 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 854 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 855 | setAtomic(NotAtomic); |
| 856 | AssertOK(); |
| 857 | setName(Name); |
| 858 | } |
| 859 | |
| 860 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 861 | BasicBlock *InsertAE) |
| 862 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 863 | Load, Ptr, InsertAE) { |
| 864 | setVolatile(isVolatile); |
| 865 | setAlignment(0); |
| 866 | setAtomic(NotAtomic); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 867 | AssertOK(); |
| 868 | setName(Name); |
| 869 | } |
| 870 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 871 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 872 | unsigned Align, Instruction *InsertBef) |
| 873 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 874 | Load, Ptr, InsertBef) { |
| 875 | setVolatile(isVolatile); |
| 876 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 877 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 878 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 879 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 882 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 883 | unsigned Align, BasicBlock *InsertAE) |
| 884 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 885 | Load, Ptr, InsertAE) { |
| 886 | setVolatile(isVolatile); |
| 887 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 888 | setAtomic(NotAtomic); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 889 | AssertOK(); |
| 890 | setName(Name); |
| 891 | } |
| 892 | |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 893 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 894 | unsigned Align, AtomicOrdering Order, |
| 895 | SynchronizationScope SynchScope, |
| 896 | Instruction *InsertBef) |
| 897 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 898 | Load, Ptr, InsertBef) { |
| 899 | setVolatile(isVolatile); |
| 900 | setAlignment(Align); |
| 901 | setAtomic(Order, SynchScope); |
| 902 | AssertOK(); |
| 903 | setName(Name); |
| 904 | } |
| 905 | |
| 906 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 907 | unsigned Align, AtomicOrdering Order, |
| 908 | SynchronizationScope SynchScope, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 909 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 910 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 911 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 912 | setVolatile(isVolatile); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 913 | setAlignment(Align); |
| 914 | setAtomic(Order, SynchScope); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 915 | AssertOK(); |
| 916 | setName(Name); |
| 917 | } |
| 918 | |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 919 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 920 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 921 | Load, Ptr, InsertBef) { |
| 922 | setVolatile(false); |
| 923 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 924 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 925 | AssertOK(); |
| 926 | if (Name && Name[0]) setName(Name); |
| 927 | } |
| 928 | |
| 929 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 930 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 931 | Load, Ptr, InsertAE) { |
| 932 | setVolatile(false); |
| 933 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 934 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 935 | AssertOK(); |
| 936 | if (Name && Name[0]) setName(Name); |
| 937 | } |
| 938 | |
| 939 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 940 | Instruction *InsertBef) |
| 941 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 942 | Load, Ptr, InsertBef) { |
| 943 | setVolatile(isVolatile); |
| 944 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 945 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 946 | AssertOK(); |
| 947 | if (Name && Name[0]) setName(Name); |
| 948 | } |
| 949 | |
| 950 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 951 | BasicBlock *InsertAE) |
| 952 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 953 | Load, Ptr, InsertAE) { |
| 954 | setVolatile(isVolatile); |
| 955 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 956 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 957 | AssertOK(); |
| 958 | if (Name && Name[0]) setName(Name); |
| 959 | } |
| 960 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 961 | void LoadInst::setAlignment(unsigned Align) { |
| 962 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 963 | assert(Align <= MaximumAlignment && |
| 964 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 965 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 966 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 967 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 968 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 969 | |
| 970 | //===----------------------------------------------------------------------===// |
| 971 | // StoreInst Implementation |
| 972 | //===----------------------------------------------------------------------===// |
| 973 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 974 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 975 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 976 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 977 | "Ptr must have pointer type!"); |
| 978 | assert(getOperand(0)->getType() == |
| 979 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 980 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 981 | assert(!(isAtomic() && getAlignment() == 0) && |
| 982 | "Alignment required for atomic load"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 983 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 984 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 985 | |
| 986 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 987 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 988 | OperandTraits<StoreInst>::op_begin(this), |
| 989 | OperandTraits<StoreInst>::operands(this), |
| 990 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 991 | Op<0>() = val; |
| 992 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 993 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 994 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 995 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 996 | AssertOK(); |
| 997 | } |
| 998 | |
| 999 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1000 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1001 | OperandTraits<StoreInst>::op_begin(this), |
| 1002 | OperandTraits<StoreInst>::operands(this), |
| 1003 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1004 | Op<0>() = val; |
| 1005 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1006 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1007 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1008 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1009 | AssertOK(); |
| 1010 | } |
| 1011 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1012 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1013 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1014 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1015 | OperandTraits<StoreInst>::op_begin(this), |
| 1016 | OperandTraits<StoreInst>::operands(this), |
| 1017 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1018 | Op<0>() = val; |
| 1019 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1020 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1021 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1022 | setAtomic(NotAtomic); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1023 | AssertOK(); |
| 1024 | } |
| 1025 | |
| 1026 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1027 | unsigned Align, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1028 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1029 | OperandTraits<StoreInst>::op_begin(this), |
| 1030 | OperandTraits<StoreInst>::operands(this), |
| 1031 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1032 | Op<0>() = val; |
| 1033 | Op<1>() = addr; |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1034 | setVolatile(isVolatile); |
| 1035 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1036 | setAtomic(NotAtomic); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1037 | AssertOK(); |
| 1038 | } |
| 1039 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1040 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1041 | unsigned Align, AtomicOrdering Order, |
| 1042 | SynchronizationScope SynchScope, |
| 1043 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1044 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1045 | OperandTraits<StoreInst>::op_begin(this), |
| 1046 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1047 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1048 | Op<0>() = val; |
| 1049 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1050 | setVolatile(isVolatile); |
| 1051 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1052 | setAtomic(Order, SynchScope); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1053 | AssertOK(); |
| 1054 | } |
| 1055 | |
| 1056 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1057 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1058 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1059 | OperandTraits<StoreInst>::op_begin(this), |
| 1060 | OperandTraits<StoreInst>::operands(this), |
| 1061 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1062 | Op<0>() = val; |
| 1063 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1064 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1065 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1066 | setAtomic(NotAtomic); |
| 1067 | AssertOK(); |
| 1068 | } |
| 1069 | |
| 1070 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1071 | unsigned Align, BasicBlock *InsertAtEnd) |
| 1072 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1073 | OperandTraits<StoreInst>::op_begin(this), |
| 1074 | OperandTraits<StoreInst>::operands(this), |
| 1075 | InsertAtEnd) { |
| 1076 | Op<0>() = val; |
| 1077 | Op<1>() = addr; |
| 1078 | setVolatile(isVolatile); |
| 1079 | setAlignment(Align); |
| 1080 | setAtomic(NotAtomic); |
| 1081 | AssertOK(); |
| 1082 | } |
| 1083 | |
| 1084 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1085 | unsigned Align, AtomicOrdering Order, |
| 1086 | SynchronizationScope SynchScope, |
| 1087 | BasicBlock *InsertAtEnd) |
| 1088 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1089 | OperandTraits<StoreInst>::op_begin(this), |
| 1090 | OperandTraits<StoreInst>::operands(this), |
| 1091 | InsertAtEnd) { |
| 1092 | Op<0>() = val; |
| 1093 | Op<1>() = addr; |
| 1094 | setVolatile(isVolatile); |
| 1095 | setAlignment(Align); |
| 1096 | setAtomic(Order, SynchScope); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1097 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1100 | void StoreInst::setAlignment(unsigned Align) { |
| 1101 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1102 | assert(Align <= MaximumAlignment && |
| 1103 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1104 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1105 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1106 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1109 | //===----------------------------------------------------------------------===// |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1110 | // AtomicCmpXchgInst Implementation |
| 1111 | //===----------------------------------------------------------------------===// |
| 1112 | |
| 1113 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1114 | AtomicOrdering Ordering, |
| 1115 | SynchronizationScope SynchScope) { |
| 1116 | Op<0>() = Ptr; |
| 1117 | Op<1>() = Cmp; |
| 1118 | Op<2>() = NewVal; |
| 1119 | setOrdering(Ordering); |
| 1120 | setSynchScope(SynchScope); |
| 1121 | |
| 1122 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1123 | "All operands must be non-null!"); |
| 1124 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1125 | "Ptr must have pointer type!"); |
| 1126 | assert(getOperand(1)->getType() == |
| 1127 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1128 | && "Ptr must be a pointer to Cmp type!"); |
| 1129 | assert(getOperand(2)->getType() == |
| 1130 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1131 | && "Ptr must be a pointer to NewVal type!"); |
| 1132 | assert(Ordering != NotAtomic && |
| 1133 | "AtomicCmpXchg instructions must be atomic!"); |
| 1134 | } |
| 1135 | |
| 1136 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1137 | AtomicOrdering Ordering, |
| 1138 | SynchronizationScope SynchScope, |
| 1139 | Instruction *InsertBefore) |
| 1140 | : Instruction(Cmp->getType(), AtomicCmpXchg, |
| 1141 | OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1142 | OperandTraits<AtomicCmpXchgInst>::operands(this), |
| 1143 | InsertBefore) { |
| 1144 | Init(Ptr, Cmp, NewVal, Ordering, SynchScope); |
| 1145 | } |
| 1146 | |
| 1147 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1148 | AtomicOrdering Ordering, |
| 1149 | SynchronizationScope SynchScope, |
| 1150 | BasicBlock *InsertAtEnd) |
| 1151 | : Instruction(Cmp->getType(), AtomicCmpXchg, |
| 1152 | OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1153 | OperandTraits<AtomicCmpXchgInst>::operands(this), |
| 1154 | InsertAtEnd) { |
| 1155 | Init(Ptr, Cmp, NewVal, Ordering, SynchScope); |
| 1156 | } |
| 1157 | |
| 1158 | //===----------------------------------------------------------------------===// |
| 1159 | // AtomicRMWInst Implementation |
| 1160 | //===----------------------------------------------------------------------===// |
| 1161 | |
| 1162 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1163 | AtomicOrdering Ordering, |
| 1164 | SynchronizationScope SynchScope) { |
| 1165 | Op<0>() = Ptr; |
| 1166 | Op<1>() = Val; |
| 1167 | setOperation(Operation); |
| 1168 | setOrdering(Ordering); |
| 1169 | setSynchScope(SynchScope); |
| 1170 | |
| 1171 | assert(getOperand(0) && getOperand(1) && |
| 1172 | "All operands must be non-null!"); |
| 1173 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1174 | "Ptr must have pointer type!"); |
| 1175 | assert(getOperand(1)->getType() == |
| 1176 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1177 | && "Ptr must be a pointer to Val type!"); |
| 1178 | assert(Ordering != NotAtomic && |
| 1179 | "AtomicRMW instructions must be atomic!"); |
| 1180 | } |
| 1181 | |
| 1182 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1183 | AtomicOrdering Ordering, |
| 1184 | SynchronizationScope SynchScope, |
| 1185 | Instruction *InsertBefore) |
| 1186 | : Instruction(Val->getType(), AtomicRMW, |
| 1187 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1188 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1189 | InsertBefore) { |
| 1190 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1191 | } |
| 1192 | |
| 1193 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1194 | AtomicOrdering Ordering, |
| 1195 | SynchronizationScope SynchScope, |
| 1196 | BasicBlock *InsertAtEnd) |
| 1197 | : Instruction(Val->getType(), AtomicRMW, |
| 1198 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1199 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1200 | InsertAtEnd) { |
| 1201 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1202 | } |
| 1203 | |
| 1204 | //===----------------------------------------------------------------------===// |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1205 | // FenceInst Implementation |
| 1206 | //===----------------------------------------------------------------------===// |
| 1207 | |
| 1208 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1209 | SynchronizationScope SynchScope, |
| 1210 | Instruction *InsertBefore) |
| 1211 | : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) { |
| 1212 | setOrdering(Ordering); |
| 1213 | setSynchScope(SynchScope); |
| 1214 | } |
| 1215 | |
| 1216 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1217 | SynchronizationScope SynchScope, |
| 1218 | BasicBlock *InsertAtEnd) |
| 1219 | : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) { |
| 1220 | setOrdering(Ordering); |
| 1221 | setSynchScope(SynchScope); |
| 1222 | } |
| 1223 | |
| 1224 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1225 | // GetElementPtrInst Implementation |
| 1226 | //===----------------------------------------------------------------------===// |
| 1227 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1228 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1229 | const Twine &Name) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1230 | assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?"); |
| 1231 | OperandList[0] = Ptr; |
| 1232 | std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1); |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1233 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1236 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1237 | : Instruction(GEPI.getType(), GetElementPtr, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1238 | OperandTraits<GetElementPtrInst>::op_end(this) |
| 1239 | - GEPI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1240 | GEPI.getNumOperands()) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1241 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1242 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1245 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1246 | /// a gep instruction with the specified parameters. |
| 1247 | /// |
| 1248 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1249 | /// indices, either as Value* or uint64_t. |
| 1250 | /// |
| 1251 | /// A null type is returned if the indices are invalid for the specified |
| 1252 | /// pointer type. |
| 1253 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1254 | template <typename IndexTy> |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1255 | static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1256 | PointerType *PTy = dyn_cast<PointerType>(Ptr); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1257 | if (!PTy) return 0; // Type isn't a pointer type! |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1258 | Type *Agg = PTy->getElementType(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1260 | // Handle the special case of the empty set index set, which is always valid. |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1261 | if (IdxList.empty()) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1262 | return Agg; |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1263 | |
| 1264 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1265 | // it cannot be 'stepped over'. |
| 1266 | if (!Agg->isSized()) |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1267 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1268 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1269 | unsigned CurIdx = 1; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1270 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1271 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1272 | if (!CT || CT->isPointerTy()) return 0; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1273 | IndexTy Index = IdxList[CurIdx]; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1274 | if (!CT->indexValid(Index)) return 0; |
| 1275 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1276 | } |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1277 | return CurIdx == IdxList.size() ? Agg : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1280 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) { |
| 1281 | return getIndexedTypeInternal(Ptr, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1284 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1285 | ArrayRef<Constant *> IdxList) { |
| 1286 | return getIndexedTypeInternal(Ptr, IdxList); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1289 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) { |
| 1290 | return getIndexedTypeInternal(Ptr, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1293 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1294 | /// zeros. If so, the result pointer and the first operand have the same |
| 1295 | /// value, just potentially different types. |
| 1296 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1297 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1298 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1299 | if (!CI->isZero()) return false; |
| 1300 | } else { |
| 1301 | return false; |
| 1302 | } |
| 1303 | } |
| 1304 | return true; |
| 1305 | } |
| 1306 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1307 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1308 | /// constant integers. If so, the result pointer and the first operand have |
| 1309 | /// a constant offset between them. |
| 1310 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1311 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1312 | if (!isa<ConstantInt>(getOperand(i))) |
| 1313 | return false; |
| 1314 | } |
| 1315 | return true; |
| 1316 | } |
| 1317 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1318 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1319 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1320 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1321 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1322 | bool GetElementPtrInst::isInBounds() const { |
| 1323 | return cast<GEPOperator>(this)->isInBounds(); |
| 1324 | } |
| 1325 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1326 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1327 | // ExtractElementInst Implementation |
| 1328 | //===----------------------------------------------------------------------===// |
| 1329 | |
| 1330 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1331 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1332 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1333 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1334 | ExtractElement, |
| 1335 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1336 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1337 | assert(isValidOperands(Val, Index) && |
| 1338 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1339 | Op<0>() = Val; |
| 1340 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1341 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1345 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1346 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1347 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1348 | ExtractElement, |
| 1349 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1350 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1351 | assert(isValidOperands(Val, Index) && |
| 1352 | "Invalid extractelement instruction operands!"); |
| 1353 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1354 | Op<0>() = Val; |
| 1355 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1356 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1359 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1360 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1361 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32)) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1362 | return false; |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
| 1366 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1367 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1368 | // InsertElementInst Implementation |
| 1369 | //===----------------------------------------------------------------------===// |
| 1370 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1371 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1372 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1373 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1374 | : Instruction(Vec->getType(), InsertElement, |
| 1375 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1376 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1377 | assert(isValidOperands(Vec, Elt, Index) && |
| 1378 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1379 | Op<0>() = Vec; |
| 1380 | Op<1>() = Elt; |
| 1381 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1382 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1385 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1386 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1387 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1388 | : Instruction(Vec->getType(), InsertElement, |
| 1389 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1390 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1391 | assert(isValidOperands(Vec, Elt, Index) && |
| 1392 | "Invalid insertelement instruction operands!"); |
| 1393 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1394 | Op<0>() = Vec; |
| 1395 | Op<1>() = Elt; |
| 1396 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1397 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1400 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1401 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1402 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1403 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1404 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1405 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1406 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1407 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1408 | if (!Index->getType()->isIntegerTy(32)) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1409 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1410 | return true; |
| 1411 | } |
| 1412 | |
| 1413 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1414 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1415 | // ShuffleVectorInst Implementation |
| 1416 | //===----------------------------------------------------------------------===// |
| 1417 | |
| 1418 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1419 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1420 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1421 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1422 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1423 | ShuffleVector, |
| 1424 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1425 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1426 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1427 | assert(isValidOperands(V1, V2, Mask) && |
| 1428 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1429 | Op<0>() = V1; |
| 1430 | Op<1>() = V2; |
| 1431 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1432 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1436 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1437 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1438 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1439 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1440 | ShuffleVector, |
| 1441 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1442 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1443 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1444 | assert(isValidOperands(V1, V2, Mask) && |
| 1445 | "Invalid shuffle vector instruction operands!"); |
| 1446 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1447 | Op<0>() = V1; |
| 1448 | Op<1>() = V2; |
| 1449 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1450 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1453 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1454 | const Value *Mask) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1455 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1456 | return false; |
| 1457 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1458 | VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1459 | if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1460 | return false; |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1461 | |
| 1462 | // Check to see if Mask is valid. |
| 1463 | if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1464 | VectorType *VTy = cast<VectorType>(V1->getType()); |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1465 | for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { |
| 1466 | if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) { |
| 1467 | if (CI->uge(VTy->getNumElements()*2)) |
| 1468 | return false; |
| 1469 | } else if (!isa<UndefValue>(MV->getOperand(i))) { |
| 1470 | return false; |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask)) |
| 1475 | return false; |
| 1476 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1477 | return true; |
| 1478 | } |
| 1479 | |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1480 | /// getMaskValue - Return the index from the shuffle mask for the specified |
| 1481 | /// output result. This is either -1 if the element is undef or a number less |
| 1482 | /// than 2*numelements. |
| 1483 | int ShuffleVectorInst::getMaskValue(unsigned i) const { |
| 1484 | const Constant *Mask = cast<Constant>(getOperand(2)); |
| 1485 | if (isa<UndefValue>(Mask)) return -1; |
| 1486 | if (isa<ConstantAggregateZero>(Mask)) return 0; |
| 1487 | const ConstantVector *MaskCV = cast<ConstantVector>(Mask); |
| 1488 | assert(i < MaskCV->getNumOperands() && "Index out of range"); |
| 1489 | |
| 1490 | if (isa<UndefValue>(MaskCV->getOperand(i))) |
| 1491 | return -1; |
| 1492 | return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue(); |
| 1493 | } |
| 1494 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1495 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1496 | // InsertValueInst Class |
| 1497 | //===----------------------------------------------------------------------===// |
| 1498 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1499 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
| 1500 | const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1501 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1502 | |
| 1503 | // There's no fundamental reason why we require at least one index |
| 1504 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1505 | // getelementptr's init routine for example). But there's no |
| 1506 | // present need to support it. |
| 1507 | assert(Idxs.size() > 0 && "InsertValueInst must have at least one index"); |
| 1508 | |
| 1509 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1510 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1511 | Op<0>() = Agg; |
| 1512 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1513 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1514 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1515 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1519 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1520 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1521 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1522 | Op<0>() = IVI.getOperand(0); |
| 1523 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1524 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1528 | // ExtractValueInst Class |
| 1529 | //===----------------------------------------------------------------------===// |
| 1530 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1531 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1532 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1533 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1534 | // There's no fundamental reason why we require at least one index. |
| 1535 | // But there's no present need to support it. |
| 1536 | assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1537 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1538 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1539 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1543 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1544 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1545 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1548 | // getIndexedType - Returns the type of the element that would be extracted |
| 1549 | // with an extractvalue instruction with the specified parameters. |
| 1550 | // |
| 1551 | // A null type is returned if the indices are invalid for the specified |
| 1552 | // pointer type. |
| 1553 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1554 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1555 | ArrayRef<unsigned> Idxs) { |
| 1556 | for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1557 | unsigned Index = Idxs[CurIdx]; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1558 | // We can't use CompositeType::indexValid(Index) here. |
| 1559 | // indexValid() always returns true for arrays because getelementptr allows |
| 1560 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 1561 | // insertvalue we need to check array indexing manually. |
| 1562 | // Since the only other types we can index into are struct types it's just |
| 1563 | // as easy to check those manually as well. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1564 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1565 | if (Index >= AT->getNumElements()) |
| 1566 | return 0; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1567 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1568 | if (Index >= ST->getNumElements()) |
| 1569 | return 0; |
| 1570 | } else { |
| 1571 | // Not a valid type to index into. |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1576 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1577 | return const_cast<Type*>(Agg); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1578 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1579 | |
| 1580 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1581 | // BinaryOperator Class |
| 1582 | //===----------------------------------------------------------------------===// |
| 1583 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1584 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1585 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1586 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1587 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1588 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1589 | OperandTraits<BinaryOperator>::operands(this), |
| 1590 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1591 | Op<0>() = S1; |
| 1592 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1593 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1594 | setName(Name); |
| 1595 | } |
| 1596 | |
| 1597 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1598 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1599 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1600 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1601 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1602 | OperandTraits<BinaryOperator>::operands(this), |
| 1603 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1604 | Op<0>() = S1; |
| 1605 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1606 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1607 | setName(Name); |
| 1608 | } |
| 1609 | |
| 1610 | |
| 1611 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1612 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 1613 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1614 | assert(LHS->getType() == RHS->getType() && |
| 1615 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1616 | #ifndef NDEBUG |
| 1617 | switch (iType) { |
| 1618 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1619 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1620 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1621 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1622 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1623 | "Tried to create an integer operation on a non-integer type!"); |
| 1624 | break; |
| 1625 | case FAdd: case FSub: |
| 1626 | case FMul: |
| 1627 | assert(getType() == LHS->getType() && |
| 1628 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1629 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1630 | "Tried to create a floating-point operation on a " |
| 1631 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1632 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1633 | case UDiv: |
| 1634 | case SDiv: |
| 1635 | assert(getType() == LHS->getType() && |
| 1636 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1637 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1638 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1639 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1640 | break; |
| 1641 | case FDiv: |
| 1642 | assert(getType() == LHS->getType() && |
| 1643 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1644 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1645 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1646 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1647 | case URem: |
| 1648 | case SRem: |
| 1649 | assert(getType() == LHS->getType() && |
| 1650 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1651 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1652 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1653 | "Incorrect operand type (not integer) for S/UREM"); |
| 1654 | break; |
| 1655 | case FRem: |
| 1656 | assert(getType() == LHS->getType() && |
| 1657 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1658 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1659 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1660 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1661 | case Shl: |
| 1662 | case LShr: |
| 1663 | case AShr: |
| 1664 | assert(getType() == LHS->getType() && |
| 1665 | "Shift operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1666 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1667 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1668 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1669 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1670 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1671 | case And: case Or: |
| 1672 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1673 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1674 | "Logical operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1675 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1676 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1677 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1678 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1679 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1680 | default: |
| 1681 | break; |
| 1682 | } |
| 1683 | #endif |
| 1684 | } |
| 1685 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1686 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1687 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1688 | Instruction *InsertBefore) { |
| 1689 | assert(S1->getType() == S2->getType() && |
| 1690 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1691 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1694 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1695 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1696 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1697 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1698 | InsertAtEnd->getInstList().push_back(Res); |
| 1699 | return Res; |
| 1700 | } |
| 1701 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1702 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1703 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1704 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1705 | return new BinaryOperator(Instruction::Sub, |
| 1706 | zero, Op, |
| 1707 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1710 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1711 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1712 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1713 | return new BinaryOperator(Instruction::Sub, |
| 1714 | zero, Op, |
| 1715 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 1718 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1719 | Instruction *InsertBefore) { |
| 1720 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1721 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 1722 | } |
| 1723 | |
| 1724 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1725 | BasicBlock *InsertAtEnd) { |
| 1726 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1727 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 1728 | } |
| 1729 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 1730 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1731 | Instruction *InsertBefore) { |
| 1732 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1733 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 1734 | } |
| 1735 | |
| 1736 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1737 | BasicBlock *InsertAtEnd) { |
| 1738 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1739 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 1740 | } |
| 1741 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1742 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1743 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1744 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1745 | return new BinaryOperator(Instruction::FSub, |
| 1746 | zero, Op, |
| 1747 | Op->getType(), Name, InsertBefore); |
| 1748 | } |
| 1749 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1750 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1751 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1752 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1753 | return new BinaryOperator(Instruction::FSub, |
| 1754 | zero, Op, |
| 1755 | Op->getType(), Name, InsertAtEnd); |
| 1756 | } |
| 1757 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1758 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1759 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1760 | Constant *C; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1761 | if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1762 | C = Constant::getAllOnesValue(PTy->getElementType()); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1763 | C = ConstantVector::get( |
Owen Anderson | f945a9e | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 1764 | std::vector<Constant*>(PTy->getNumElements(), C)); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1765 | } else { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1766 | C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1770 | Op->getType(), Name, InsertBefore); |
| 1771 | } |
| 1772 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1773 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1774 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1775 | Constant *AllOnes; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1776 | if (VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1777 | // Create a vector of all ones values. |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1778 | Constant *Elt = Constant::getAllOnesValue(PTy->getElementType()); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1779 | AllOnes = ConstantVector::get( |
Owen Anderson | f945a9e | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 1780 | std::vector<Constant*>(PTy->getNumElements(), Elt)); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1781 | } else { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1782 | AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1786 | Op->getType(), Name, InsertAtEnd); |
| 1787 | } |
| 1788 | |
| 1789 | |
| 1790 | // isConstantAllOnes - Helper function for several functions below |
| 1791 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1792 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 1793 | return CI->isAllOnesValue(); |
| 1794 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 1795 | return CV->isAllOnesValue(); |
| 1796 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1799 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1800 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1801 | if (Bop->getOpcode() == Instruction::Sub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1802 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
| 1803 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1804 | return false; |
| 1805 | } |
| 1806 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1807 | bool BinaryOperator::isFNeg(const Value *V) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1808 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1809 | if (Bop->getOpcode() == Instruction::FSub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1810 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
Dan Gohman | 11ff570 | 2009-09-11 00:05:10 +0000 | [diff] [blame] | 1811 | return C->isNegativeZeroValue(); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1812 | return false; |
| 1813 | } |
| 1814 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1815 | bool BinaryOperator::isNot(const Value *V) { |
| 1816 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1817 | return (Bop->getOpcode() == Instruction::Xor && |
| 1818 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1819 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1820 | return false; |
| 1821 | } |
| 1822 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1823 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1824 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1827 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1828 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1831 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1832 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 1833 | } |
| 1834 | |
| 1835 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 1836 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 1837 | } |
| 1838 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1839 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1840 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1841 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1842 | Value *Op0 = BO->getOperand(0); |
| 1843 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1844 | if (isConstantAllOnes(Op0)) return Op1; |
| 1845 | |
| 1846 | assert(isConstantAllOnes(Op1)); |
| 1847 | return Op0; |
| 1848 | } |
| 1849 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1850 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1851 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | |
| 1855 | // swapOperands - Exchange the two operands to this instruction. This |
| 1856 | // instruction is safe to use on any binary instruction and does not |
| 1857 | // modify the semantics of the instruction. If the instruction is |
| 1858 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1859 | // |
| 1860 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1861 | if (!isCommutative()) |
| 1862 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 1863 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1864 | return false; |
| 1865 | } |
| 1866 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1867 | void BinaryOperator::setHasNoUnsignedWrap(bool b) { |
| 1868 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); |
| 1869 | } |
| 1870 | |
| 1871 | void BinaryOperator::setHasNoSignedWrap(bool b) { |
| 1872 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); |
| 1873 | } |
| 1874 | |
| 1875 | void BinaryOperator::setIsExact(bool b) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 1876 | cast<PossiblyExactOperator>(this)->setIsExact(b); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1879 | bool BinaryOperator::hasNoUnsignedWrap() const { |
| 1880 | return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); |
| 1881 | } |
| 1882 | |
| 1883 | bool BinaryOperator::hasNoSignedWrap() const { |
| 1884 | return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); |
| 1885 | } |
| 1886 | |
| 1887 | bool BinaryOperator::isExact() const { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 1888 | return cast<PossiblyExactOperator>(this)->isExact(); |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1891 | //===----------------------------------------------------------------------===// |
| 1892 | // CastInst Class |
| 1893 | //===----------------------------------------------------------------------===// |
| 1894 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1895 | // Just determine if this cast only deals with integral->integral conversion. |
| 1896 | bool CastInst::isIntegerCast() const { |
| 1897 | switch (getOpcode()) { |
| 1898 | default: return false; |
| 1899 | case Instruction::ZExt: |
| 1900 | case Instruction::SExt: |
| 1901 | case Instruction::Trunc: |
| 1902 | return true; |
| 1903 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1904 | return getOperand(0)->getType()->isIntegerTy() && |
| 1905 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1906 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1909 | bool CastInst::isLosslessCast() const { |
| 1910 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1911 | if (getOpcode() != Instruction::BitCast) |
| 1912 | return false; |
| 1913 | |
| 1914 | // Identity cast is always lossless |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1915 | Type* SrcTy = getOperand(0)->getType(); |
| 1916 | Type* DstTy = getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1917 | if (SrcTy == DstTy) |
| 1918 | return true; |
| 1919 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1920 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1921 | if (SrcTy->isPointerTy()) |
| 1922 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1923 | return false; // Other types have no identity values |
| 1924 | } |
| 1925 | |
| 1926 | /// This function determines if the CastInst does not require any bits to be |
| 1927 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1928 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1929 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 1930 | /// # bitcast i32* %x to i8* |
| 1931 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 1932 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1933 | /// @brief Determine if the described cast is a no-op. |
| 1934 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1935 | Type *SrcTy, |
| 1936 | Type *DestTy, |
| 1937 | Type *IntPtrTy) { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1938 | switch (Opcode) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1939 | default: |
| 1940 | assert(!"Invalid CastOp"); |
| 1941 | case Instruction::Trunc: |
| 1942 | case Instruction::ZExt: |
| 1943 | case Instruction::SExt: |
| 1944 | case Instruction::FPTrunc: |
| 1945 | case Instruction::FPExt: |
| 1946 | case Instruction::UIToFP: |
| 1947 | case Instruction::SIToFP: |
| 1948 | case Instruction::FPToUI: |
| 1949 | case Instruction::FPToSI: |
| 1950 | return false; // These always modify bits |
| 1951 | case Instruction::BitCast: |
| 1952 | return true; // BitCast never modifies bits. |
| 1953 | case Instruction::PtrToInt: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1954 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1955 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1956 | case Instruction::IntToPtr: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1957 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1958 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1962 | /// @brief Determine if a cast is a no-op. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1963 | bool CastInst::isNoopCast(Type *IntPtrTy) const { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1964 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 1965 | } |
| 1966 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1967 | /// This function determines if a pair of casts can be eliminated and what |
| 1968 | /// opcode should be used in the elimination. This assumes that there are two |
| 1969 | /// instructions like this: |
| 1970 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1971 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1972 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1973 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1974 | /// If no such cast is permited, the function returns 0. |
| 1975 | unsigned CastInst::isEliminableCastPair( |
| 1976 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1977 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *IntPtrTy) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1978 | { |
| 1979 | // Define the 144 possibilities for these two cast instructions. The values |
| 1980 | // in this matrix determine what to do in a given situation and select the |
| 1981 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1982 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1983 | // the following cast properties: |
| 1984 | // |
| 1985 | // Size Compare Source Destination |
| 1986 | // Operator Src ? Size Type Sign Type Sign |
| 1987 | // -------- ------------ ------------------- --------------------- |
| 1988 | // TRUNC > Integer Any Integral Any |
| 1989 | // ZEXT < Integral Unsigned Integer Any |
| 1990 | // SEXT < Integral Signed Integer Any |
| 1991 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1992 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1993 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1994 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1995 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1996 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1997 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1998 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Dan Gohman | eb7111b | 2010-04-07 23:22:42 +0000 | [diff] [blame] | 1999 | // BITCAST = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2000 | // |
| 2001 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 2002 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2003 | // into "fptoui double to i64", but this loses information about the range |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2004 | // of the produced value (we no longer know the top-part is all zeros). |
| 2005 | // Further this conversion is often much more expensive for typical hardware, |
| 2006 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 2007 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2008 | const unsigned numCastOps = |
| 2009 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2010 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 2011 | // T F F U S F F P I B -+ |
| 2012 | // R Z S P P I I T P 2 N T | |
| 2013 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 2014 | // N X X U S F F N X N 2 V | |
| 2015 | // C T T I I P P C T T P T -+ |
| 2016 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 2017 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 2018 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2019 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 2020 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2021 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 2022 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 2023 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 2024 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 2025 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 2026 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 2027 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 2028 | }; |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2029 | |
| 2030 | // If either of the casts are a bitcast from scalar to vector, disallow the |
| 2031 | // merging. |
| 2032 | if ((firstOp == Instruction::BitCast && |
| 2033 | isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2034 | (secondOp == Instruction::BitCast && |
| 2035 | isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2036 | return 0; // Disallowed |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2037 | |
| 2038 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2039 | [secondOp-Instruction::CastOpsBegin]; |
| 2040 | switch (ElimCase) { |
| 2041 | case 0: |
| 2042 | // categorically disallowed |
| 2043 | return 0; |
| 2044 | case 1: |
| 2045 | // allowed, use first cast's opcode |
| 2046 | return firstOp; |
| 2047 | case 2: |
| 2048 | // allowed, use second cast's opcode |
| 2049 | return secondOp; |
| 2050 | case 3: |
| 2051 | // no-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e04b456 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2052 | // is integer and we are not converting between a vector and a |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2053 | // non vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2054 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2055 | return firstOp; |
| 2056 | return 0; |
| 2057 | case 4: |
| 2058 | // no-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2059 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2060 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2061 | return firstOp; |
| 2062 | return 0; |
| 2063 | case 5: |
| 2064 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2065 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2066 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2067 | return secondOp; |
| 2068 | return 0; |
| 2069 | case 6: |
| 2070 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2071 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2072 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2073 | return secondOp; |
| 2074 | return 0; |
| 2075 | case 7: { |
| 2076 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2077 | if (!IntPtrTy) |
| 2078 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2079 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 2080 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2081 | if (MidSize >= PtrSize) |
| 2082 | return Instruction::BitCast; |
| 2083 | return 0; |
| 2084 | } |
| 2085 | case 8: { |
| 2086 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2087 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2088 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2089 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2090 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2091 | if (SrcSize == DstSize) |
| 2092 | return Instruction::BitCast; |
| 2093 | else if (SrcSize < DstSize) |
| 2094 | return firstOp; |
| 2095 | return secondOp; |
| 2096 | } |
| 2097 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 2098 | return Instruction::ZExt; |
| 2099 | case 10: |
| 2100 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 2101 | // the same as the original, in which case its just a bitcast |
| 2102 | if (SrcTy == DstTy) |
| 2103 | return Instruction::BitCast; |
| 2104 | return 0; // If the types are not the same we can't eliminate it. |
| 2105 | case 11: |
| 2106 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 2107 | // is a pointer to pointer cast. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2108 | if (SrcTy->isPointerTy() && MidTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2109 | return secondOp; |
| 2110 | return 0; |
| 2111 | case 12: |
| 2112 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2113 | if (MidTy->isPointerTy() && DstTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2114 | return firstOp; |
| 2115 | return 0; |
| 2116 | case 13: { |
| 2117 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2118 | if (!IntPtrTy) |
| 2119 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2120 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 2121 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2122 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2123 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2124 | return Instruction::BitCast; |
| 2125 | return 0; |
| 2126 | } |
| 2127 | case 99: |
| 2128 | // cast combination can't happen (error in input). This is for all cases |
| 2129 | // where the MidTy is not the same for the two cast instructions. |
| 2130 | assert(!"Invalid Cast Combination"); |
| 2131 | return 0; |
| 2132 | default: |
| 2133 | assert(!"Error in CastResults table!!!"); |
| 2134 | return 0; |
| 2135 | } |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2139 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2140 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2141 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2142 | // Construct and return the appropriate CastInst subclass |
| 2143 | switch (op) { |
| 2144 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2145 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2146 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2147 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2148 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2149 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2150 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2151 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2152 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2153 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2154 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2155 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2156 | default: |
| 2157 | assert(!"Invalid opcode provided"); |
| 2158 | } |
| 2159 | return 0; |
| 2160 | } |
| 2161 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2162 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2163 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2164 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2165 | // Construct and return the appropriate CastInst subclass |
| 2166 | switch (op) { |
| 2167 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2168 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2169 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2170 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2171 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2172 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2173 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2174 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2175 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2176 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2177 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2178 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2179 | default: |
| 2180 | assert(!"Invalid opcode provided"); |
| 2181 | } |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2185 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2186 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2187 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2188 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2189 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2190 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2193 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2194 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2195 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2196 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2197 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2198 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2201 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2202 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2203 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2204 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2205 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2206 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2209 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2210 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2211 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2212 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2213 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2214 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2217 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2218 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2219 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2220 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2221 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2222 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2225 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2226 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2227 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2228 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2229 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2230 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2233 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2234 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2235 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2236 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2237 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2238 | "Invalid cast"); |
| 2239 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2240 | if (Ty->isIntegerTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2241 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 2242 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2246 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2247 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2248 | Instruction *InsertBefore) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2249 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2250 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2251 | "Invalid cast"); |
| 2252 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2253 | if (Ty->isIntegerTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2254 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2255 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2258 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2259 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2260 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2261 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2262 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2263 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2264 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2265 | Instruction::CastOps opcode = |
| 2266 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2267 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2268 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2269 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2272 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2273 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2274 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2275 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2276 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2277 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2278 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2279 | Instruction::CastOps opcode = |
| 2280 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2281 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2282 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2283 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2286 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2287 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2288 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2289 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2290 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2291 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2292 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2293 | Instruction::CastOps opcode = |
| 2294 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2295 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2296 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2299 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2300 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2301 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2302 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2303 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2304 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2305 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2306 | Instruction::CastOps opcode = |
| 2307 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2308 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2309 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2312 | // Check whether it is valid to call getCastOpcode for these types. |
| 2313 | // This routine must be kept in sync with getCastOpcode. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2314 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2315 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2316 | return false; |
| 2317 | |
| 2318 | if (SrcTy == DestTy) |
| 2319 | return true; |
| 2320 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2321 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2322 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2323 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2324 | // An element by element cast. Valid if casting the elements is valid. |
| 2325 | SrcTy = SrcVecTy->getElementType(); |
| 2326 | DestTy = DestVecTy->getElementType(); |
| 2327 | } |
| 2328 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2329 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2330 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2331 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2332 | |
| 2333 | // Run through the possibilities ... |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2334 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2335 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2336 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2337 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2338 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2339 | } else if (SrcTy->isVectorTy()) { // Casting from vector |
| 2340 | return DestBits == SrcBits; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2341 | } else { // Casting from something else |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2342 | return SrcTy->isPointerTy(); |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2343 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2344 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2345 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2346 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2347 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2348 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2349 | } else if (SrcTy->isVectorTy()) { // Casting from vector |
| 2350 | return DestBits == SrcBits; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2351 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2352 | return false; |
| 2353 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2354 | } else if (DestTy->isVectorTy()) { // Casting to vector |
| 2355 | return DestBits == SrcBits; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2356 | } else if (DestTy->isPointerTy()) { // Casting to pointer |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2357 | if (SrcTy->isPointerTy()) { // Casting from pointer |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2358 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2359 | } else if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2360 | return true; |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2361 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2362 | return false; |
| 2363 | } |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 2364 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2365 | if (SrcTy->isVectorTy()) { |
| 2366 | return DestBits == SrcBits; // 64-bit vector to MMX |
Duncan Sands | 2d3cdd6 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 2367 | } else { |
| 2368 | return false; |
| 2369 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2370 | } else { // Casting to something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2371 | return false; |
| 2372 | } |
| 2373 | } |
| 2374 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2375 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2376 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2377 | // logic in the castIsValid function below. This axiom should hold: |
| 2378 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2379 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2380 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2381 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2382 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2383 | CastInst::getCastOpcode( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2384 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 2385 | Type *SrcTy = Src->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2386 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2387 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2388 | "Only first class types are castable!"); |
| 2389 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2390 | if (SrcTy == DestTy) |
| 2391 | return BitCast; |
| 2392 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2393 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2394 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2395 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2396 | // An element by element cast. Find the appropriate opcode based on the |
| 2397 | // element types. |
| 2398 | SrcTy = SrcVecTy->getElementType(); |
| 2399 | DestTy = DestVecTy->getElementType(); |
| 2400 | } |
| 2401 | |
| 2402 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2403 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2404 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2405 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2406 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2407 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2408 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2409 | if (DestBits < SrcBits) |
| 2410 | return Trunc; // int -> smaller int |
| 2411 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2412 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2413 | return SExt; // signed -> SEXT |
| 2414 | else |
| 2415 | return ZExt; // unsigned -> ZEXT |
| 2416 | } else { |
| 2417 | return BitCast; // Same size, No-op cast |
| 2418 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2419 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2420 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2421 | return FPToSI; // FP -> sint |
| 2422 | else |
| 2423 | return FPToUI; // FP -> uint |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2424 | } else if (SrcTy->isVectorTy()) { |
| 2425 | assert(DestBits == SrcBits && |
| 2426 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2427 | return BitCast; // Same size, no-op cast |
| 2428 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2429 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2430 | "Casting from a value that is not first-class type"); |
| 2431 | return PtrToInt; // ptr -> int |
| 2432 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2433 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2434 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2435 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2436 | return SIToFP; // sint -> FP |
| 2437 | else |
| 2438 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2439 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2440 | if (DestBits < SrcBits) { |
| 2441 | return FPTrunc; // FP -> smaller FP |
| 2442 | } else if (DestBits > SrcBits) { |
| 2443 | return FPExt; // FP -> larger FP |
| 2444 | } else { |
| 2445 | return BitCast; // same size, no-op cast |
| 2446 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2447 | } else if (SrcTy->isVectorTy()) { |
| 2448 | assert(DestBits == SrcBits && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2449 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2450 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2451 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2452 | llvm_unreachable("Casting pointer or non-first class to float"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2453 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2454 | } else if (DestTy->isVectorTy()) { |
| 2455 | assert(DestBits == SrcBits && |
| 2456 | "Illegal cast to vector (wrong type or size)"); |
| 2457 | return BitCast; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2458 | } else if (DestTy->isPointerTy()) { |
| 2459 | if (SrcTy->isPointerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2460 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2461 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2462 | return IntToPtr; // int -> ptr |
| 2463 | } else { |
| 2464 | assert(!"Casting pointer to other than pointer or int"); |
| 2465 | } |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2466 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2467 | if (SrcTy->isVectorTy()) { |
| 2468 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2469 | return BitCast; // 64-bit vector to MMX |
| 2470 | } else { |
| 2471 | assert(!"Illegal cast to X86_MMX"); |
| 2472 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2473 | } else { |
| 2474 | assert(!"Casting to type that is not first-class"); |
| 2475 | } |
| 2476 | |
| 2477 | // If we fall through to here we probably hit an assertion cast above |
| 2478 | // and assertions are not turned on. Anything we return is an error, so |
| 2479 | // BitCast is as good a choice as any. |
| 2480 | return BitCast; |
| 2481 | } |
| 2482 | |
| 2483 | //===----------------------------------------------------------------------===// |
| 2484 | // CastInst SubClass Constructors |
| 2485 | //===----------------------------------------------------------------------===// |
| 2486 | |
| 2487 | /// Check that the construction parameters for a CastInst are correct. This |
| 2488 | /// could be broken out into the separate constructors but it is useful to have |
| 2489 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 2490 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2491 | bool |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2492 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2493 | |
| 2494 | // Check for type sanity on the arguments |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2495 | Type *SrcTy = S->getType(); |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 2496 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 2497 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2498 | return false; |
| 2499 | |
| 2500 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2501 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 2502 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2503 | |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2504 | // If these are vector types, get the lengths of the vectors (using zero for |
| 2505 | // scalar types means that checking that vector lengths match also checks that |
| 2506 | // scalars are not being converted to vectors or vectors to scalars). |
| 2507 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 2508 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 2509 | unsigned DstLength = DstTy->isVectorTy() ? |
| 2510 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 2511 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2512 | // Switch on the opcode provided |
| 2513 | switch (op) { |
| 2514 | default: return false; // This is an input error |
| 2515 | case Instruction::Trunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2516 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2517 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2518 | case Instruction::ZExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2519 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2520 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2521 | case Instruction::SExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2522 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2523 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2524 | case Instruction::FPTrunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2525 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2526 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2527 | case Instruction::FPExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2528 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2529 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2530 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2531 | case Instruction::SIToFP: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2532 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2533 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2534 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2535 | case Instruction::FPToSI: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2536 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2537 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2538 | case Instruction::PtrToInt: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2539 | return SrcTy->isPointerTy() && DstTy->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2540 | case Instruction::IntToPtr: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2541 | return SrcTy->isIntegerTy() && DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2542 | case Instruction::BitCast: |
| 2543 | // BitCast implies a no-op cast of type only. No bits change. |
| 2544 | // However, you can't cast pointers to anything but pointers. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2545 | if (SrcTy->isPointerTy() != DstTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2546 | return false; |
| 2547 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2548 | // Now we know we're not dealing with a pointer/non-pointer mismatch. In all |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2549 | // these cases, the cast is okay if the source and destination bit widths |
| 2550 | // are identical. |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2551 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2556 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2557 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2558 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2562 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2563 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2564 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2568 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2569 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2570 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2574 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2575 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2576 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2577 | } |
| 2578 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2579 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2580 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2581 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2584 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2585 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2586 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2587 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2591 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2592 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2593 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2597 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2598 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2599 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2603 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2604 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2605 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2609 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2610 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2611 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2615 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2616 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2617 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2621 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2622 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2623 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2627 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2628 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2629 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2633 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2634 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2635 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2639 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2640 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2641 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2645 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2646 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2647 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2651 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2652 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2653 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2657 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2658 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2659 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2663 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2664 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2665 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2669 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2670 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2671 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2675 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2676 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2677 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2681 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2682 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2683 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2687 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2688 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2689 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2693 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2694 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2695 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2696 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 2697 | |
| 2698 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2699 | // CmpInst Classes |
| 2700 | //===----------------------------------------------------------------------===// |
| 2701 | |
Chris Lattner | aec33da | 2010-01-22 06:25:37 +0000 | [diff] [blame] | 2702 | void CmpInst::Anchor() const {} |
| 2703 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2704 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2705 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2706 | Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2707 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2708 | OperandTraits<CmpInst>::op_begin(this), |
| 2709 | OperandTraits<CmpInst>::operands(this), |
| 2710 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2711 | Op<0>() = LHS; |
| 2712 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 2713 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2714 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2715 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2717 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2718 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2719 | BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2720 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2721 | OperandTraits<CmpInst>::op_begin(this), |
| 2722 | OperandTraits<CmpInst>::operands(this), |
| 2723 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2724 | Op<0>() = LHS; |
| 2725 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 2726 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2727 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | CmpInst * |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2731 | CmpInst::Create(OtherOps Op, unsigned short predicate, |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2732 | Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2733 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2734 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2735 | if (InsertBefore) |
| 2736 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2737 | S1, S2, Name); |
| 2738 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2739 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2740 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2741 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2742 | |
| 2743 | if (InsertBefore) |
| 2744 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2745 | S1, S2, Name); |
| 2746 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2747 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2748 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | CmpInst * |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2752 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2753 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2754 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2755 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2756 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2757 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2758 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2759 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | void CmpInst::swapOperands() { |
| 2763 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2764 | IC->swapOperands(); |
| 2765 | else |
| 2766 | cast<FCmpInst>(this)->swapOperands(); |
| 2767 | } |
| 2768 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 2769 | bool CmpInst::isCommutative() const { |
| 2770 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2771 | return IC->isCommutative(); |
| 2772 | return cast<FCmpInst>(this)->isCommutative(); |
| 2773 | } |
| 2774 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 2775 | bool CmpInst::isEquality() const { |
| 2776 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2777 | return IC->isEquality(); |
| 2778 | return cast<FCmpInst>(this)->isEquality(); |
| 2779 | } |
| 2780 | |
| 2781 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2782 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2783 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2784 | default: assert(!"Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2785 | case ICMP_EQ: return ICMP_NE; |
| 2786 | case ICMP_NE: return ICMP_EQ; |
| 2787 | case ICMP_UGT: return ICMP_ULE; |
| 2788 | case ICMP_ULT: return ICMP_UGE; |
| 2789 | case ICMP_UGE: return ICMP_ULT; |
| 2790 | case ICMP_ULE: return ICMP_UGT; |
| 2791 | case ICMP_SGT: return ICMP_SLE; |
| 2792 | case ICMP_SLT: return ICMP_SGE; |
| 2793 | case ICMP_SGE: return ICMP_SLT; |
| 2794 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2795 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2796 | case FCMP_OEQ: return FCMP_UNE; |
| 2797 | case FCMP_ONE: return FCMP_UEQ; |
| 2798 | case FCMP_OGT: return FCMP_ULE; |
| 2799 | case FCMP_OLT: return FCMP_UGE; |
| 2800 | case FCMP_OGE: return FCMP_ULT; |
| 2801 | case FCMP_OLE: return FCMP_UGT; |
| 2802 | case FCMP_UEQ: return FCMP_ONE; |
| 2803 | case FCMP_UNE: return FCMP_OEQ; |
| 2804 | case FCMP_UGT: return FCMP_OLE; |
| 2805 | case FCMP_ULT: return FCMP_OGE; |
| 2806 | case FCMP_UGE: return FCMP_OLT; |
| 2807 | case FCMP_ULE: return FCMP_OGT; |
| 2808 | case FCMP_ORD: return FCMP_UNO; |
| 2809 | case FCMP_UNO: return FCMP_ORD; |
| 2810 | case FCMP_TRUE: return FCMP_FALSE; |
| 2811 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2812 | } |
| 2813 | } |
| 2814 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2815 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2816 | switch (pred) { |
| 2817 | default: assert(! "Unknown icmp predicate!"); |
| 2818 | case ICMP_EQ: case ICMP_NE: |
| 2819 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2820 | return pred; |
| 2821 | case ICMP_UGT: return ICMP_SGT; |
| 2822 | case ICMP_ULT: return ICMP_SLT; |
| 2823 | case ICMP_UGE: return ICMP_SGE; |
| 2824 | case ICMP_ULE: return ICMP_SLE; |
| 2825 | } |
| 2826 | } |
| 2827 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 2828 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 2829 | switch (pred) { |
| 2830 | default: assert(! "Unknown icmp predicate!"); |
| 2831 | case ICMP_EQ: case ICMP_NE: |
| 2832 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 2833 | return pred; |
| 2834 | case ICMP_SGT: return ICMP_UGT; |
| 2835 | case ICMP_SLT: return ICMP_ULT; |
| 2836 | case ICMP_SGE: return ICMP_UGE; |
| 2837 | case ICMP_SLE: return ICMP_ULE; |
| 2838 | } |
| 2839 | } |
| 2840 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2841 | /// Initialize a set of values that all satisfy the condition with C. |
| 2842 | /// |
| 2843 | ConstantRange |
| 2844 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 2845 | APInt Lower(C); |
| 2846 | APInt Upper(C); |
| 2847 | uint32_t BitWidth = C.getBitWidth(); |
| 2848 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2849 | default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!"); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2850 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 2851 | case ICmpInst::ICMP_NE: Lower++; break; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2852 | case ICmpInst::ICMP_ULT: |
| 2853 | Lower = APInt::getMinValue(BitWidth); |
| 2854 | // Check for an empty-set condition. |
| 2855 | if (Lower == Upper) |
| 2856 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 2857 | break; |
| 2858 | case ICmpInst::ICMP_SLT: |
| 2859 | Lower = APInt::getSignedMinValue(BitWidth); |
| 2860 | // Check for an empty-set condition. |
| 2861 | if (Lower == Upper) |
| 2862 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 2863 | break; |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2864 | case ICmpInst::ICMP_UGT: |
| 2865 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2866 | // Check for an empty-set condition. |
| 2867 | if (Lower == Upper) |
| 2868 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2869 | break; |
| 2870 | case ICmpInst::ICMP_SGT: |
| 2871 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2872 | // Check for an empty-set condition. |
| 2873 | if (Lower == Upper) |
| 2874 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2875 | break; |
| 2876 | case ICmpInst::ICMP_ULE: |
| 2877 | Lower = APInt::getMinValue(BitWidth); Upper++; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2878 | // Check for a full-set condition. |
| 2879 | if (Lower == Upper) |
| 2880 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2881 | break; |
| 2882 | case ICmpInst::ICMP_SLE: |
| 2883 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2884 | // Check for a full-set condition. |
| 2885 | if (Lower == Upper) |
| 2886 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2887 | break; |
| 2888 | case ICmpInst::ICMP_UGE: |
| 2889 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2890 | // Check for a full-set condition. |
| 2891 | if (Lower == Upper) |
| 2892 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2893 | break; |
| 2894 | case ICmpInst::ICMP_SGE: |
| 2895 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2896 | // Check for a full-set condition. |
| 2897 | if (Lower == Upper) |
| 2898 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2899 | break; |
| 2900 | } |
| 2901 | return ConstantRange(Lower, Upper); |
| 2902 | } |
| 2903 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2904 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2905 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2906 | default: assert(!"Unknown cmp predicate!"); |
| 2907 | case ICMP_EQ: case ICMP_NE: |
| 2908 | return pred; |
| 2909 | case ICMP_SGT: return ICMP_SLT; |
| 2910 | case ICMP_SLT: return ICMP_SGT; |
| 2911 | case ICMP_SGE: return ICMP_SLE; |
| 2912 | case ICMP_SLE: return ICMP_SGE; |
| 2913 | case ICMP_UGT: return ICMP_ULT; |
| 2914 | case ICMP_ULT: return ICMP_UGT; |
| 2915 | case ICMP_UGE: return ICMP_ULE; |
| 2916 | case ICMP_ULE: return ICMP_UGE; |
| 2917 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2918 | case FCMP_FALSE: case FCMP_TRUE: |
| 2919 | case FCMP_OEQ: case FCMP_ONE: |
| 2920 | case FCMP_UEQ: case FCMP_UNE: |
| 2921 | case FCMP_ORD: case FCMP_UNO: |
| 2922 | return pred; |
| 2923 | case FCMP_OGT: return FCMP_OLT; |
| 2924 | case FCMP_OLT: return FCMP_OGT; |
| 2925 | case FCMP_OGE: return FCMP_OLE; |
| 2926 | case FCMP_OLE: return FCMP_OGE; |
| 2927 | case FCMP_UGT: return FCMP_ULT; |
| 2928 | case FCMP_ULT: return FCMP_UGT; |
| 2929 | case FCMP_UGE: return FCMP_ULE; |
| 2930 | case FCMP_ULE: return FCMP_UGE; |
| 2931 | } |
| 2932 | } |
| 2933 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2934 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2935 | switch (predicate) { |
| 2936 | default: return false; |
| 2937 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2938 | case ICmpInst::ICMP_UGE: return true; |
| 2939 | } |
| 2940 | } |
| 2941 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 2942 | bool CmpInst::isSigned(unsigned short predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2943 | switch (predicate) { |
| 2944 | default: return false; |
| 2945 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2946 | case ICmpInst::ICMP_SGE: return true; |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2951 | switch (predicate) { |
| 2952 | default: return false; |
| 2953 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2954 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2955 | case FCmpInst::FCMP_ORD: return true; |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2960 | switch (predicate) { |
| 2961 | default: return false; |
| 2962 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2963 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2964 | case FCmpInst::FCMP_UNO: return true; |
| 2965 | } |
| 2966 | } |
| 2967 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 2968 | bool CmpInst::isTrueWhenEqual(unsigned short predicate) { |
| 2969 | switch(predicate) { |
| 2970 | default: return false; |
| 2971 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 2972 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | bool CmpInst::isFalseWhenEqual(unsigned short predicate) { |
| 2977 | switch(predicate) { |
| 2978 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 2979 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 2980 | default: return false; |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2985 | //===----------------------------------------------------------------------===// |
| 2986 | // SwitchInst Implementation |
| 2987 | //===----------------------------------------------------------------------===// |
| 2988 | |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 2989 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 2990 | assert(Value && Default && NumReserved); |
| 2991 | ReservedSpace = NumReserved; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2992 | NumOperands = 2; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2993 | OperandList = allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2994 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2995 | OperandList[0] = Value; |
| 2996 | OperandList[1] = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2999 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3000 | /// switch on and a default destination. The number of additional cases can |
| 3001 | /// be specified here to make memory allocation more efficient. This |
| 3002 | /// constructor can also autoinsert before another instruction. |
| 3003 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3004 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3005 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3006 | 0, 0, InsertBefore) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3007 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3011 | /// switch on and a default destination. The number of additional cases can |
| 3012 | /// be specified here to make memory allocation more efficient. This |
| 3013 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3014 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3015 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3016 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3017 | 0, 0, InsertAtEnd) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3018 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3021 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3022 | : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) { |
| 3023 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
| 3024 | NumOperands = SI.getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3025 | Use *OL = OperandList, *InOL = SI.OperandList; |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3026 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3027 | OL[i] = InOL[i]; |
| 3028 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3029 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3030 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 3033 | SwitchInst::~SwitchInst() { |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3034 | dropHungoffUses(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3038 | /// addCase - Add an entry to the switch instruction... |
| 3039 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3040 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3041 | unsigned OpNo = NumOperands; |
| 3042 | if (OpNo+2 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3043 | growOperands(); // Get more space! |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3044 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3045 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3046 | NumOperands = OpNo+2; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3047 | OperandList[OpNo] = OnVal; |
| 3048 | OperandList[OpNo+1] = Dest; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | /// removeCase - This method removes the specified successor from the switch |
| 3052 | /// instruction. Note that this cannot be used to remove the default |
| 3053 | /// destination (successor #0). |
| 3054 | /// |
| 3055 | void SwitchInst::removeCase(unsigned idx) { |
| 3056 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3057 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 3058 | |
| 3059 | unsigned NumOps = getNumOperands(); |
| 3060 | Use *OL = OperandList; |
| 3061 | |
Jay Foad | 1427772 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3062 | // Overwrite this case with the end of the list. |
| 3063 | if ((idx + 1) * 2 != NumOps) { |
| 3064 | OL[idx * 2] = OL[NumOps - 2]; |
| 3065 | OL[idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | // Nuke the last value. |
| 3069 | OL[NumOps-2].set(0); |
| 3070 | OL[NumOps-2+1].set(0); |
| 3071 | NumOperands = NumOps-2; |
| 3072 | } |
| 3073 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3074 | /// growOperands - grow operands - This grows the operand list in response |
| 3075 | /// to a push_back style of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3076 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3077 | void SwitchInst::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3078 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3079 | unsigned NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3080 | |
| 3081 | ReservedSpace = NumOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3082 | Use *NewOps = allocHungoffUses(NumOps); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3083 | Use *OldOps = OperandList; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3084 | for (unsigned i = 0; i != e; ++i) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3085 | NewOps[i] = OldOps[i]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3086 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3087 | OperandList = NewOps; |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3088 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | |
| 3092 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 3093 | return getSuccessor(idx); |
| 3094 | } |
| 3095 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 3096 | return getNumSuccessors(); |
| 3097 | } |
| 3098 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 3099 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3100 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3101 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3102 | //===----------------------------------------------------------------------===// |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3103 | // IndirectBrInst Implementation |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3104 | //===----------------------------------------------------------------------===// |
| 3105 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3106 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3107 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3108 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3109 | ReservedSpace = 1+NumDests; |
| 3110 | NumOperands = 1; |
| 3111 | OperandList = allocHungoffUses(ReservedSpace); |
| 3112 | |
| 3113 | OperandList[0] = Address; |
| 3114 | } |
| 3115 | |
| 3116 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3117 | /// growOperands - grow operands - This grows the operand list in response |
| 3118 | /// to a push_back style of operation. This grows the number of ops by 2 times. |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3119 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3120 | void IndirectBrInst::growOperands() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3121 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3122 | unsigned NumOps = e*2; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3123 | |
| 3124 | ReservedSpace = NumOps; |
| 3125 | Use *NewOps = allocHungoffUses(NumOps); |
| 3126 | Use *OldOps = OperandList; |
| 3127 | for (unsigned i = 0; i != e; ++i) |
| 3128 | NewOps[i] = OldOps[i]; |
| 3129 | OperandList = NewOps; |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3130 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3133 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3134 | Instruction *InsertBefore) |
| 3135 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3136 | 0, 0, InsertBefore) { |
| 3137 | init(Address, NumCases); |
| 3138 | } |
| 3139 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3140 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3141 | BasicBlock *InsertAtEnd) |
| 3142 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3143 | 0, 0, InsertAtEnd) { |
| 3144 | init(Address, NumCases); |
| 3145 | } |
| 3146 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3147 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
| 3148 | : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3149 | allocHungoffUses(IBI.getNumOperands()), |
| 3150 | IBI.getNumOperands()) { |
| 3151 | Use *OL = OperandList, *InOL = IBI.OperandList; |
| 3152 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3153 | OL[i] = InOL[i]; |
| 3154 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3155 | } |
| 3156 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3157 | IndirectBrInst::~IndirectBrInst() { |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3158 | dropHungoffUses(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | /// addDestination - Add a destination. |
| 3162 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3163 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3164 | unsigned OpNo = NumOperands; |
| 3165 | if (OpNo+1 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3166 | growOperands(); // Get more space! |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3167 | // Initialize some new operands. |
| 3168 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 3169 | NumOperands = OpNo+1; |
| 3170 | OperandList[OpNo] = DestBB; |
| 3171 | } |
| 3172 | |
| 3173 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3174 | /// indirectbr instruction. |
| 3175 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3176 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
| 3177 | |
| 3178 | unsigned NumOps = getNumOperands(); |
| 3179 | Use *OL = OperandList; |
| 3180 | |
| 3181 | // Replace this value with the last one. |
| 3182 | OL[idx+1] = OL[NumOps-1]; |
| 3183 | |
| 3184 | // Nuke the last value. |
| 3185 | OL[NumOps-1].set(0); |
| 3186 | NumOperands = NumOps-1; |
| 3187 | } |
| 3188 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3189 | BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3190 | return getSuccessor(idx); |
| 3191 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3192 | unsigned IndirectBrInst::getNumSuccessorsV() const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3193 | return getNumSuccessors(); |
| 3194 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3195 | void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3196 | setSuccessor(idx, B); |
| 3197 | } |
| 3198 | |
| 3199 | //===----------------------------------------------------------------------===// |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3200 | // clone_impl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3201 | //===----------------------------------------------------------------------===// |
| 3202 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3203 | // Define these methods here so vtables don't get emitted into every translation |
| 3204 | // unit that uses these classes. |
| 3205 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3206 | GetElementPtrInst *GetElementPtrInst::clone_impl() const { |
| 3207 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3210 | BinaryOperator *BinaryOperator::clone_impl() const { |
| 3211 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3214 | FCmpInst* FCmpInst::clone_impl() const { |
| 3215 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3218 | ICmpInst* ICmpInst::clone_impl() const { |
| 3219 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3222 | ExtractValueInst *ExtractValueInst::clone_impl() const { |
| 3223 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3226 | InsertValueInst *InsertValueInst::clone_impl() const { |
| 3227 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3230 | AllocaInst *AllocaInst::clone_impl() const { |
| 3231 | return new AllocaInst(getAllocatedType(), |
| 3232 | (Value*)getOperand(0), |
| 3233 | getAlignment()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3236 | LoadInst *LoadInst::clone_impl() const { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3237 | return new LoadInst(getOperand(0), Twine(), isVolatile(), |
| 3238 | getAlignment(), getOrdering(), getSynchScope()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3241 | StoreInst *StoreInst::clone_impl() const { |
Eli Friedman | cad9f2a | 2011-08-10 17:39:11 +0000 | [diff] [blame^] | 3242 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3243 | getAlignment(), getOrdering(), getSynchScope()); |
| 3244 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3247 | AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const { |
| 3248 | AtomicCmpXchgInst *Result = |
| 3249 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
| 3250 | getOrdering(), getSynchScope()); |
| 3251 | Result->setVolatile(isVolatile()); |
| 3252 | return Result; |
| 3253 | } |
| 3254 | |
| 3255 | AtomicRMWInst *AtomicRMWInst::clone_impl() const { |
| 3256 | AtomicRMWInst *Result = |
| 3257 | new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1), |
| 3258 | getOrdering(), getSynchScope()); |
| 3259 | Result->setVolatile(isVolatile()); |
| 3260 | return Result; |
| 3261 | } |
| 3262 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3263 | FenceInst *FenceInst::clone_impl() const { |
| 3264 | return new FenceInst(getContext(), getOrdering(), getSynchScope()); |
| 3265 | } |
| 3266 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3267 | TruncInst *TruncInst::clone_impl() const { |
| 3268 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3271 | ZExtInst *ZExtInst::clone_impl() const { |
| 3272 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3275 | SExtInst *SExtInst::clone_impl() const { |
| 3276 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3279 | FPTruncInst *FPTruncInst::clone_impl() const { |
| 3280 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3283 | FPExtInst *FPExtInst::clone_impl() const { |
| 3284 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3287 | UIToFPInst *UIToFPInst::clone_impl() const { |
| 3288 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3291 | SIToFPInst *SIToFPInst::clone_impl() const { |
| 3292 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3295 | FPToUIInst *FPToUIInst::clone_impl() const { |
| 3296 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3299 | FPToSIInst *FPToSIInst::clone_impl() const { |
| 3300 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3303 | PtrToIntInst *PtrToIntInst::clone_impl() const { |
| 3304 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3307 | IntToPtrInst *IntToPtrInst::clone_impl() const { |
| 3308 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3309 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3310 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3311 | BitCastInst *BitCastInst::clone_impl() const { |
| 3312 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3313 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3314 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3315 | CallInst *CallInst::clone_impl() const { |
| 3316 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3319 | SelectInst *SelectInst::clone_impl() const { |
| 3320 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3321 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3322 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3323 | VAArgInst *VAArgInst::clone_impl() const { |
| 3324 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3325 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3326 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3327 | ExtractElementInst *ExtractElementInst::clone_impl() const { |
| 3328 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3329 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3330 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3331 | InsertElementInst *InsertElementInst::clone_impl() const { |
| 3332 | return InsertElementInst::Create(getOperand(0), |
| 3333 | getOperand(1), |
| 3334 | getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3337 | ShuffleVectorInst *ShuffleVectorInst::clone_impl() const { |
| 3338 | return new ShuffleVectorInst(getOperand(0), |
| 3339 | getOperand(1), |
| 3340 | getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3341 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3342 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3343 | PHINode *PHINode::clone_impl() const { |
| 3344 | return new PHINode(*this); |
| 3345 | } |
| 3346 | |
| 3347 | ReturnInst *ReturnInst::clone_impl() const { |
| 3348 | return new(getNumOperands()) ReturnInst(*this); |
| 3349 | } |
| 3350 | |
| 3351 | BranchInst *BranchInst::clone_impl() const { |
Jay Foad | d81f3c9 | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 3352 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3353 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3354 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3355 | SwitchInst *SwitchInst::clone_impl() const { |
| 3356 | return new SwitchInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3359 | IndirectBrInst *IndirectBrInst::clone_impl() const { |
| 3360 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3364 | InvokeInst *InvokeInst::clone_impl() const { |
| 3365 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3366 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3367 | |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3368 | ResumeInst *ResumeInst::clone_impl() const { |
| 3369 | return new(1) ResumeInst(*this); |
| 3370 | } |
| 3371 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3372 | UnwindInst *UnwindInst::clone_impl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 3373 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3374 | return new UnwindInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3377 | UnreachableInst *UnreachableInst::clone_impl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 3378 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3379 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3380 | } |