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