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