blob: 5cd52f7721c3e0883fbc3fa021ccc72d94dc6bbb [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000157/// hasConstantOrUndefValue - Whether the specified PHI node always merges
158/// together the same value, assuming that undefs result in the same value as
159/// non-undefs.
160/// Unlike \ref hasConstantValue, this does not return a value because the
161/// unique non-undef incoming value need not dominate the PHI node.
162bool PHINode::hasConstantOrUndefValue() const {
163 Value *ConstantValue = nullptr;
164 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
165 Value *Incoming = getIncomingValue(i);
166 if (Incoming != this && !isa<UndefValue>(Incoming)) {
167 if (ConstantValue && ConstantValue != Incoming)
168 return false;
169 ConstantValue = Incoming;
170 }
171 }
172 return true;
173}
174
Bill Wendlingfae14752011-08-12 20:24:12 +0000175//===----------------------------------------------------------------------===//
176// LandingPadInst Implementation
177//===----------------------------------------------------------------------===//
178
David Majnemer7fddecc2015-06-17 20:52:32 +0000179LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
180 const Twine &NameStr, Instruction *InsertBefore)
181 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
182 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000183}
184
David Majnemer7fddecc2015-06-17 20:52:32 +0000185LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
186 const Twine &NameStr, BasicBlock *InsertAtEnd)
187 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
188 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000189}
190
191LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000192 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
193 LP.getNumOperands()),
194 ReservedSpace(LP.getNumOperands()) {
195 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000196 Use *OL = getOperandList();
197 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000198 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
199 OL[I] = InOL[I];
200
201 setCleanup(LP.isCleanup());
202}
203
David Majnemer7fddecc2015-06-17 20:52:32 +0000204LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000205 const Twine &NameStr,
206 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000207 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000208}
209
David Majnemer7fddecc2015-06-17 20:52:32 +0000210LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000211 const Twine &NameStr,
212 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000213 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000214}
215
David Majnemer7fddecc2015-06-17 20:52:32 +0000216void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000217 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000218 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000219 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000220 setName(NameStr);
221 setCleanup(false);
222}
223
224/// growOperands - grow operands - This grows the operand list in response to a
225/// push_back style of operation. This grows the number of ops by 2 times.
226void LandingPadInst::growOperands(unsigned Size) {
227 unsigned e = getNumOperands();
228 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000229 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000230 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000231}
232
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000233void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000234 unsigned OpNo = getNumOperands();
235 growOperands(1);
236 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000237 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000238 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000239}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240
241//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242// CallInst Implementation
243//===----------------------------------------------------------------------===//
244
Gordon Henriksen14a55692007-12-10 02:14:30 +0000245CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000246}
247
David Blaikie348de692015-04-23 21:36:23 +0000248void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000249 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000250 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000251 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
252 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000254
Jay Foad5bd375a2011-07-15 08:37:34 +0000255#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000256 assert((Args.size() == FTy->getNumParams() ||
257 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000258 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000259
260 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000261 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000262 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000263 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264#endif
265
266 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000267
268 auto It = populateBundleOperandInfos(Bundles, Args.size());
269 (void)It;
270 assert(It + 1 == op_end() && "Should add up!");
271
Jay Foad5bd375a2011-07-15 08:37:34 +0000272 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273}
274
Jay Foad5bd375a2011-07-15 08:37:34 +0000275void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000276 FTy =
277 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000278 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000279 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000280
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000282
283 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284}
285
Daniel Dunbar4975db62009-07-25 04:41:11 +0000286CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287 Instruction *InsertBefore)
288 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000290 Instruction::Call,
291 OperandTraits<CallInst>::op_end(this) - 1,
292 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000293 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294}
295
Daniel Dunbar4975db62009-07-25 04:41:11 +0000296CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 BasicBlock *InsertAtEnd)
298 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
299 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000300 Instruction::Call,
301 OperandTraits<CallInst>::op_end(this) - 1,
302 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000303 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304}
305
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000307 : Instruction(CI.getType(), Instruction::Call,
308 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
309 CI.getNumOperands()),
310 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000311 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000312 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000313
Jay Foad5bd375a2011-07-15 08:37:34 +0000314 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000315 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
316 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000317 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318}
319
Sanjoy Das2d161452015-11-18 06:23:38 +0000320CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
321 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000322 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000323
324 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
325 InsertPt);
326 NewCI->setTailCallKind(CI->getTailCallKind());
327 NewCI->setCallingConv(CI->getCallingConv());
328 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000329 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000330 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000331 return NewCI;
332}
333
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000334void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000335 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000336 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000337 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000338}
339
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000340void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
341 AttributeSet PAL = getAttributes();
342 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
343 setAttributes(PAL);
344}
345
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000346void CallInst::removeAttribute(unsigned i, Attribute::AttrKind attr) {
347 AttributeSet PAL = getAttributes();
348 PAL = PAL.removeAttribute(getContext(), i, attr);
349 setAttributes(PAL);
350}
351
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000352void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000353 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000354 AttrBuilder B(attr);
355 LLVMContext &Context = getContext();
356 PAL = PAL.removeAttributes(Context, i,
357 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000358 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000359}
360
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000361void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
362 AttributeSet PAL = getAttributes();
363 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
364 setAttributes(PAL);
365}
366
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000367void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
368 AttributeSet PAL = getAttributes();
369 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
370 setAttributes(PAL);
371}
372
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000373bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000374 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
375
Bill Wendling749a43d2012-12-30 13:50:49 +0000376 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000377 return true;
378 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000379 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000380 return false;
381}
382
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000383bool CallInst::dataOperandHasImpliedAttr(unsigned i,
384 Attribute::AttrKind A) const {
385
Sanjoy Das776e4a72015-11-05 01:53:26 +0000386 // There are getNumOperands() - 1 data operands. The last operand is the
387 // callee.
388 assert(i < getNumOperands() && "Data operand index out of bounds!");
389
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000390 // The attribute A can either be directly specified, if the operand in
391 // question is a call argument; or be indirectly implied by the kind of its
392 // containing operand bundle, if the operand is a bundle operand.
393
394 if (i < (getNumArgOperands() + 1))
395 return paramHasAttr(i, A);
396
397 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
398 "Must be either a call argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000399 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000400}
401
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000402/// IsConstantOne - Return true only if val is constant int 1
403static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000404 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000405 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
406 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000407}
408
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000409static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000410 BasicBlock *InsertAtEnd, Type *IntPtrTy,
411 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000412 Value *ArraySize, Function *MallocF,
413 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000414 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000415 "createMalloc needs either InsertBefore or InsertAtEnd");
416
417 // malloc(type) becomes:
418 // bitcast (i8* malloc(typeSize)) to type*
419 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000420 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000421 if (!ArraySize)
422 ArraySize = ConstantInt::get(IntPtrTy, 1);
423 else if (ArraySize->getType() != IntPtrTy) {
424 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000425 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
426 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000427 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000428 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
429 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000430 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000431
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000432 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000433 if (IsConstantOne(AllocSize)) {
434 AllocSize = ArraySize; // Operand * 1 = Operand
435 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
436 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
437 false /*ZExt*/);
438 // Malloc arg is constant product of type size and array size
439 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
440 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000441 // Multiply type size by the array size...
442 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000443 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
444 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000445 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000446 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
447 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000448 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000449 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000450
Victor Hernandez788eaab2009-09-18 19:20:02 +0000451 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000452 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000453 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
454 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000455 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000456 Value *MallocFunc = MallocF;
457 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000458 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000459 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000460 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000461 CallInst *MCall = nullptr;
462 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000463 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000464 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000465 Result = MCall;
466 if (Result->getType() != AllocPtrType)
467 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000468 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000469 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000470 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000471 Result = MCall;
472 if (Result->getType() != AllocPtrType) {
473 InsertAtEnd->getInstList().push_back(MCall);
474 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000475 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000476 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000477 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000478 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000479 if (Function *F = dyn_cast<Function>(MallocFunc)) {
480 MCall->setCallingConv(F->getCallingConv());
481 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
482 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000483 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000484
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000485 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000486}
487
488/// CreateMalloc - Generate the IR for a call to malloc:
489/// 1. Compute the malloc call's argument as the specified type's size,
490/// possibly multiplied by the array size if the array size is not
491/// constant 1.
492/// 2. Call malloc with that argument.
493/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000494Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000495 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000496 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000497 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000498 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000499 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000500 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000501}
502
503/// CreateMalloc - Generate the IR for a call to malloc:
504/// 1. Compute the malloc call's argument as the specified type's size,
505/// possibly multiplied by the array size if the array size is not
506/// constant 1.
507/// 2. Call malloc with that argument.
508/// 3. Bitcast the result of the malloc call to the specified type.
509/// Note: This function does not add the bitcast to the basic block, that is the
510/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000511Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000512 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000513 Value *AllocSize, Value *ArraySize,
514 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000515 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000516 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000517}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000518
Ana Pazosb3596022016-02-03 21:34:39 +0000519static Instruction *createFree(Value *Source, Instruction *InsertBefore,
Victor Hernandeze2971492009-10-24 04:23:03 +0000520 BasicBlock *InsertAtEnd) {
521 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
522 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000523 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000524 "Can not free something of nonpointer type!");
525
Ana Pazosb3596022016-02-03 21:34:39 +0000526 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
527 Module *M = BB->getParent()->getParent();
Victor Hernandeze2971492009-10-24 04:23:03 +0000528
Chris Lattner229907c2011-07-18 04:54:35 +0000529 Type *VoidTy = Type::getVoidTy(M->getContext());
530 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000531 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000532 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Ana Pazosb3596022016-02-03 21:34:39 +0000533 CallInst *Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000534 Value *PtrCast = Source;
535 if (InsertBefore) {
536 if (Source->getType() != IntPtrTy)
537 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
538 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
539 } else {
540 if (Source->getType() != IntPtrTy)
541 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
542 Result = CallInst::Create(FreeFunc, PtrCast, "");
543 }
544 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000545 if (Function *F = dyn_cast<Function>(FreeFunc))
546 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000547
548 return Result;
549}
550
551/// CreateFree - Generate the IR for a call to the builtin free function.
Ana Pazosb3596022016-02-03 21:34:39 +0000552Instruction *CallInst::CreateFree(Value *Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000553 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000554}
555
556/// CreateFree - Generate the IR for a call to the builtin free function.
557/// Note: This function does not add the call to the basic block, that is the
558/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000559Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
560 Instruction *FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000561 assert(FreeCall && "CreateFree did not create a CallInst");
562 return FreeCall;
563}
564
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000565//===----------------------------------------------------------------------===//
566// InvokeInst Implementation
567//===----------------------------------------------------------------------===//
568
David Blaikie3e807092015-05-13 18:35:26 +0000569void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
570 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000571 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000572 const Twine &NameStr) {
573 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000574
Sanjoy Das9303c242015-09-24 19:14:18 +0000575 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
576 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000577 Op<-3>() = Fn;
578 Op<-2>() = IfNormal;
579 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000580
581#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000582 assert(((Args.size() == FTy->getNumParams()) ||
583 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000584 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000585
Jay Foad5bd375a2011-07-15 08:37:34 +0000586 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000587 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000588 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000589 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000590#endif
591
592 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000593
594 auto It = populateBundleOperandInfos(Bundles, Args.size());
595 (void)It;
596 assert(It + 3 == op_end() && "Should add up!");
597
Jay Foad5bd375a2011-07-15 08:37:34 +0000598 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599}
600
Misha Brukmanb1c93172005-04-21 23:48:37 +0000601InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000602 : TerminatorInst(II.getType(), Instruction::Invoke,
603 OperandTraits<InvokeInst>::op_end(this) -
604 II.getNumOperands(),
605 II.getNumOperands()),
606 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000607 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000608 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000609 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
610 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000611 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000612}
613
Sanjoy Das2d161452015-11-18 06:23:38 +0000614InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
615 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000616 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000617
618 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
619 II->getUnwindDest(), Args, OpB,
620 II->getName(), InsertPt);
621 NewII->setCallingConv(II->getCallingConv());
622 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000623 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000624 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000625 return NewII;
626}
627
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
629 return getSuccessor(idx);
630}
631unsigned InvokeInst::getNumSuccessorsV() const {
632 return getNumSuccessors();
633}
634void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
635 return setSuccessor(idx, B);
636}
637
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000638bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000639 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
640
Bill Wendling749a43d2012-12-30 13:50:49 +0000641 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000642 return true;
643 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000644 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000645 return false;
646}
647
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000648bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
649 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000650 // There are getNumOperands() - 3 data operands. The last three operands are
651 // the callee and the two successor basic blocks.
652 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
653
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000654 // The attribute A can either be directly specified, if the operand in
655 // question is an invoke argument; or be indirectly implied by the kind of its
656 // containing operand bundle, if the operand is a bundle operand.
657
658 if (i < (getNumArgOperands() + 1))
659 return paramHasAttr(i, A);
660
661 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
662 "Must be either an invoke argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000663 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000664}
665
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000666void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000667 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000668 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000669 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000670}
671
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000672void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind attr) {
673 AttributeSet PAL = getAttributes();
674 PAL = PAL.removeAttribute(getContext(), i, attr);
675 setAttributes(PAL);
676}
677
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000678void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000679 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000680 AttrBuilder B(attr);
681 PAL = PAL.removeAttributes(getContext(), i,
682 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000683 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000684}
685
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000686void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
687 AttributeSet PAL = getAttributes();
688 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
689 setAttributes(PAL);
690}
691
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000692void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
693 AttributeSet PAL = getAttributes();
694 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
695 setAttributes(PAL);
696}
697
Bill Wendlingfae14752011-08-12 20:24:12 +0000698LandingPadInst *InvokeInst::getLandingPadInst() const {
699 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
700}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000701
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000702//===----------------------------------------------------------------------===//
703// ReturnInst Implementation
704//===----------------------------------------------------------------------===//
705
Chris Lattner2195fc42007-02-24 00:55:48 +0000706ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000707 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000708 OperandTraits<ReturnInst>::op_end(this) -
709 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000710 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000711 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000712 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000713 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000714}
715
Owen Anderson55f1c092009-08-13 21:58:54 +0000716ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
717 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000718 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
719 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000720 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000721 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000722}
Owen Anderson55f1c092009-08-13 21:58:54 +0000723ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
724 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000725 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
726 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000727 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000728 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000729}
Owen Anderson55f1c092009-08-13 21:58:54 +0000730ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
731 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000732 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000733}
734
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000735unsigned ReturnInst::getNumSuccessorsV() const {
736 return getNumSuccessors();
737}
738
Devang Patelae682fb2008-02-26 17:56:20 +0000739/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
740/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000742 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000743}
744
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000745BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000746 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000747}
748
Devang Patel59643e52008-02-23 00:35:18 +0000749ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000750}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000751
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000752//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000753// ResumeInst Implementation
754//===----------------------------------------------------------------------===//
755
756ResumeInst::ResumeInst(const ResumeInst &RI)
757 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
758 OperandTraits<ResumeInst>::op_begin(this), 1) {
759 Op<0>() = RI.Op<0>();
760}
761
762ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
763 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
764 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
765 Op<0>() = Exn;
766}
767
768ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
769 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
770 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
771 Op<0>() = Exn;
772}
773
774unsigned ResumeInst::getNumSuccessorsV() const {
775 return getNumSuccessors();
776}
777
778void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
779 llvm_unreachable("ResumeInst has no successors!");
780}
781
782BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
783 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000784}
785
786//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000787// CleanupReturnInst Implementation
788//===----------------------------------------------------------------------===//
789
790CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
791 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
792 OperandTraits<CleanupReturnInst>::op_end(this) -
793 CRI.getNumOperands(),
794 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000795 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000796 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000797 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000798 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000799}
800
David Majnemer8a1c45d2015-12-12 05:38:55 +0000801void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000802 if (UnwindBB)
803 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000804
David Majnemer8a1c45d2015-12-12 05:38:55 +0000805 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000806 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000807 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000808}
809
David Majnemer8a1c45d2015-12-12 05:38:55 +0000810CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
811 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000812 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
813 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000814 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
815 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000816 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000817}
818
David Majnemer8a1c45d2015-12-12 05:38:55 +0000819CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
820 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000821 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
822 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000823 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
824 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000825 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000826}
827
828BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
829 assert(Idx == 0);
830 return getUnwindDest();
831}
832unsigned CleanupReturnInst::getNumSuccessorsV() const {
833 return getNumSuccessors();
834}
835void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
836 assert(Idx == 0);
837 setUnwindDest(B);
838}
839
840//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000841// CatchReturnInst Implementation
842//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000843void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000844 Op<0>() = CatchPad;
845 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000846}
David Majnemer654e1302015-07-31 17:58:14 +0000847
848CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
849 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000850 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
851 Op<0>() = CRI.Op<0>();
852 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000853}
854
David Majnemer8a1c45d2015-12-12 05:38:55 +0000855CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000856 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000857 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000858 OperandTraits<CatchReturnInst>::op_begin(this), 2,
859 InsertBefore) {
860 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000861}
862
David Majnemer8a1c45d2015-12-12 05:38:55 +0000863CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000864 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000865 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000866 OperandTraits<CatchReturnInst>::op_begin(this), 2,
867 InsertAtEnd) {
868 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000869}
870
871BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000872 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000873 return getSuccessor();
874}
875unsigned CatchReturnInst::getNumSuccessorsV() const {
876 return getNumSuccessors();
877}
878void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000879 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000880 setSuccessor(B);
881}
882
883//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000884// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000885//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000886
887CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
888 unsigned NumReservedValues,
889 const Twine &NameStr,
890 Instruction *InsertBefore)
891 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
892 InsertBefore) {
893 if (UnwindDest)
894 ++NumReservedValues;
895 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000896 setName(NameStr);
897}
898
David Majnemer8a1c45d2015-12-12 05:38:55 +0000899CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
900 unsigned NumReservedValues,
901 const Twine &NameStr, BasicBlock *InsertAtEnd)
902 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000903 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000904 if (UnwindDest)
905 ++NumReservedValues;
906 init(ParentPad, UnwindDest, NumReservedValues + 1);
907 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000908}
909
David Majnemer8a1c45d2015-12-12 05:38:55 +0000910CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
911 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
912 CSI.getNumOperands()) {
913 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
914 setNumHungOffUseOperands(ReservedSpace);
915 Use *OL = getOperandList();
916 const Use *InOL = CSI.getOperandList();
917 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
918 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000919}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000920
921void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
922 unsigned NumReservedValues) {
923 assert(ParentPad && NumReservedValues);
924
925 ReservedSpace = NumReservedValues;
926 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
927 allocHungoffUses(ReservedSpace);
928
929 Op<0>() = ParentPad;
930 if (UnwindDest) {
931 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
932 setUnwindDest(UnwindDest);
933 }
934}
935
936/// growOperands - grow operands - This grows the operand list in response to a
937/// push_back style of operation. This grows the number of ops by 2 times.
938void CatchSwitchInst::growOperands(unsigned Size) {
939 unsigned NumOperands = getNumOperands();
940 assert(NumOperands >= 1);
941 if (ReservedSpace >= NumOperands + Size)
942 return;
943 ReservedSpace = (NumOperands + Size / 2) * 2;
944 growHungoffUses(ReservedSpace);
945}
946
947void CatchSwitchInst::addHandler(BasicBlock *Handler) {
948 unsigned OpNo = getNumOperands();
949 growOperands(1);
950 assert(OpNo < ReservedSpace && "Growing didn't work!");
951 setNumHungOffUseOperands(getNumOperands() + 1);
952 getOperandList()[OpNo] = Handler;
953}
954
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000955void CatchSwitchInst::removeHandler(handler_iterator HI) {
956 // Move all subsequent handlers up one.
957 Use *EndDst = op_end() - 1;
958 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
959 *CurDst = *(CurDst + 1);
960 // Null out the last handler use.
961 *EndDst = nullptr;
962
963 setNumHungOffUseOperands(getNumOperands() - 1);
964}
965
David Majnemer8a1c45d2015-12-12 05:38:55 +0000966BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
967 return getSuccessor(idx);
968}
969unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +0000970 return getNumSuccessors();
971}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000972void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
973 setSuccessor(idx, B);
974}
975
976//===----------------------------------------------------------------------===//
977// FuncletPadInst Implementation
978//===----------------------------------------------------------------------===//
979void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
980 const Twine &NameStr) {
981 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
982 std::copy(Args.begin(), Args.end(), op_begin());
983 setParentPad(ParentPad);
984 setName(NameStr);
985}
986
987FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
988 : Instruction(FPI.getType(), FPI.getOpcode(),
989 OperandTraits<FuncletPadInst>::op_end(this) -
990 FPI.getNumOperands(),
991 FPI.getNumOperands()) {
992 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
993 setParentPad(FPI.getParentPad());
994}
995
996FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
997 ArrayRef<Value *> Args, unsigned Values,
998 const Twine &NameStr, Instruction *InsertBefore)
999 : Instruction(ParentPad->getType(), Op,
1000 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1001 InsertBefore) {
1002 init(ParentPad, Args, NameStr);
1003}
1004
1005FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
1006 ArrayRef<Value *> Args, unsigned Values,
1007 const Twine &NameStr, BasicBlock *InsertAtEnd)
1008 : Instruction(ParentPad->getType(), Op,
1009 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
1010 InsertAtEnd) {
1011 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +00001012}
1013
1014//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001015// UnreachableInst Implementation
1016//===----------------------------------------------------------------------===//
1017
Owen Anderson55f1c092009-08-13 21:58:54 +00001018UnreachableInst::UnreachableInst(LLVMContext &Context,
1019 Instruction *InsertBefore)
1020 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001021 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001022}
Owen Anderson55f1c092009-08-13 21:58:54 +00001023UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
1024 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +00001025 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00001026}
1027
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001028unsigned UnreachableInst::getNumSuccessorsV() const {
1029 return getNumSuccessors();
1030}
1031
1032void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001033 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001034}
1035
1036BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001037 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001038}
1039
1040//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001041// BranchInst Implementation
1042//===----------------------------------------------------------------------===//
1043
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001044void BranchInst::AssertOK() {
1045 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001046 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001047 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001048}
1049
Chris Lattner2195fc42007-02-24 00:55:48 +00001050BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001051 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001052 OperandTraits<BranchInst>::op_end(this) - 1,
1053 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001054 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001055 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001056}
1057BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1058 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001059 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001060 OperandTraits<BranchInst>::op_end(this) - 3,
1061 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001062 Op<-1>() = IfTrue;
1063 Op<-2>() = IfFalse;
1064 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001065#ifndef NDEBUG
1066 AssertOK();
1067#endif
1068}
1069
1070BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001071 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001072 OperandTraits<BranchInst>::op_end(this) - 1,
1073 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001074 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001075 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001076}
1077
1078BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1079 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001080 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001081 OperandTraits<BranchInst>::op_end(this) - 3,
1082 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001083 Op<-1>() = IfTrue;
1084 Op<-2>() = IfFalse;
1085 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001086#ifndef NDEBUG
1087 AssertOK();
1088#endif
1089}
1090
1091
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001092BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001093 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001094 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1095 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001096 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001097 if (BI.getNumOperands() != 1) {
1098 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001099 Op<-3>() = BI.Op<-3>();
1100 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001101 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001102 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001103}
1104
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001105void BranchInst::swapSuccessors() {
1106 assert(isConditional() &&
1107 "Cannot swap successors of an unconditional branch");
1108 Op<-1>().swap(Op<-2>());
1109
1110 // Update profile metadata if present and it matches our structural
1111 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001112 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001113 if (!ProfileData || ProfileData->getNumOperands() != 3)
1114 return;
1115
1116 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001117 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1118 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001119 setMetadata(LLVMContext::MD_prof,
1120 MDNode::get(ProfileData->getContext(), Ops));
1121}
1122
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001123BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1124 return getSuccessor(idx);
1125}
1126unsigned BranchInst::getNumSuccessorsV() const {
1127 return getNumSuccessors();
1128}
1129void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1130 setSuccessor(idx, B);
1131}
1132
1133
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001134//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001135// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001136//===----------------------------------------------------------------------===//
1137
Owen Andersonb6b25302009-07-14 23:09:55 +00001138static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001139 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001140 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001141 else {
1142 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001143 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001144 assert(Amt->getType()->isIntegerTy() &&
1145 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001146 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001147 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001148}
1149
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001150AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1151 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001152
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001153AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1154 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001155
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001156AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001157 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001158 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001159
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001160AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001161 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001162 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001163
Chris Lattner229907c2011-07-18 04:54:35 +00001164AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001165 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001166 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1167 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1168 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001169 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001170 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001171 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001172}
1173
Chris Lattner229907c2011-07-18 04:54:35 +00001174AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001175 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001176 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1177 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1178 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001179 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001180 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001181 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001182}
1183
Gordon Henriksen14a55692007-12-10 02:14:30 +00001184// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001185AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001186}
1187
Victor Hernandez8acf2952009-10-23 21:09:37 +00001188void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001189 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001190 assert(Align <= MaximumAlignment &&
1191 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001192 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1193 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001194 assert(getAlignment() == Align && "Alignment representation error!");
1195}
1196
Victor Hernandez8acf2952009-10-23 21:09:37 +00001197bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001198 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001199 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001200 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001201}
1202
Chris Lattner8b291e62008-11-26 02:54:17 +00001203/// isStaticAlloca - Return true if this alloca is in the entry block of the
1204/// function and is a constant size. If so, the code generator will fold it
1205/// into the prolog/epilog code, so it is basically free.
1206bool AllocaInst::isStaticAlloca() const {
1207 // Must be constant size.
1208 if (!isa<ConstantInt>(getArraySize())) return false;
1209
1210 // Must be in the entry block.
1211 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001212 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001213}
1214
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001216// LoadInst Implementation
1217//===----------------------------------------------------------------------===//
1218
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001219void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001220 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001221 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001222 assert(!(isAtomic() && getAlignment() == 0) &&
1223 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001224}
1225
Daniel Dunbar4975db62009-07-25 04:41:11 +00001226LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001227 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001228
Daniel Dunbar4975db62009-07-25 04:41:11 +00001229LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001230 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001231
David Blaikie0c28fd72015-05-20 21:46:30 +00001232LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001233 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001234 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001235
1236LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1237 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001238 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001239
David Blaikieb7a029872015-04-17 19:56:21 +00001240LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001241 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001242 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1243 CrossThread, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001244
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001245LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001246 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001247 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
1248 CrossThread, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001249
David Blaikie15d9a4c2015-04-06 20:59:48 +00001250LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001251 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001252 SynchronizationScope SynchScope, Instruction *InsertBef)
1253 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001254 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001255 setVolatile(isVolatile);
1256 setAlignment(Align);
1257 setAtomic(Order, SynchScope);
1258 AssertOK();
1259 setName(Name);
1260}
1261
1262LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1263 unsigned Align, AtomicOrdering Order,
1264 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001265 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001266 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001267 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001268 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001269 setAlignment(Align);
1270 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001271 AssertOK();
1272 setName(Name);
1273}
1274
Daniel Dunbar27096822009-08-11 18:11:15 +00001275LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1276 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1277 Load, Ptr, InsertBef) {
1278 setVolatile(false);
1279 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001280 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001281 AssertOK();
1282 if (Name && Name[0]) setName(Name);
1283}
1284
1285LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1286 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1287 Load, Ptr, InsertAE) {
1288 setVolatile(false);
1289 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001290 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001291 AssertOK();
1292 if (Name && Name[0]) setName(Name);
1293}
1294
David Blaikie0c28fd72015-05-20 21:46:30 +00001295LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001296 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001297 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1298 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001299 setVolatile(isVolatile);
1300 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001301 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001302 AssertOK();
1303 if (Name && Name[0]) setName(Name);
1304}
1305
1306LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1307 BasicBlock *InsertAE)
1308 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1309 Load, Ptr, InsertAE) {
1310 setVolatile(isVolatile);
1311 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001312 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001313 AssertOK();
1314 if (Name && Name[0]) setName(Name);
1315}
1316
Christopher Lamb84485702007-04-22 19:24:39 +00001317void LoadInst::setAlignment(unsigned Align) {
1318 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001319 assert(Align <= MaximumAlignment &&
1320 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001321 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001322 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001323 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001324}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001325
1326//===----------------------------------------------------------------------===//
1327// StoreInst Implementation
1328//===----------------------------------------------------------------------===//
1329
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001330void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001331 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001332 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001333 "Ptr must have pointer type!");
1334 assert(getOperand(0)->getType() ==
1335 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001336 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001337 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001338 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001339}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001340
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001341StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001342 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001343
1344StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001345 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001346
Misha Brukmanb1c93172005-04-21 23:48:37 +00001347StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001348 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001349 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001350
1351StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001352 BasicBlock *InsertAtEnd)
1353 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1354
1355StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1356 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001357 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1358 CrossThread, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001359
1360StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1361 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001362 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
1363 CrossThread, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001364
Misha Brukmanb1c93172005-04-21 23:48:37 +00001365StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001366 unsigned Align, AtomicOrdering Order,
1367 SynchronizationScope SynchScope,
1368 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001369 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001370 OperandTraits<StoreInst>::op_begin(this),
1371 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001372 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001373 Op<0>() = val;
1374 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001375 setVolatile(isVolatile);
1376 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001377 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001378 AssertOK();
1379}
1380
1381StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001382 unsigned Align, AtomicOrdering Order,
1383 SynchronizationScope SynchScope,
1384 BasicBlock *InsertAtEnd)
1385 : Instruction(Type::getVoidTy(val->getContext()), Store,
1386 OperandTraits<StoreInst>::op_begin(this),
1387 OperandTraits<StoreInst>::operands(this),
1388 InsertAtEnd) {
1389 Op<0>() = val;
1390 Op<1>() = addr;
1391 setVolatile(isVolatile);
1392 setAlignment(Align);
1393 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001394 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001395}
1396
Christopher Lamb84485702007-04-22 19:24:39 +00001397void StoreInst::setAlignment(unsigned Align) {
1398 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001399 assert(Align <= MaximumAlignment &&
1400 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001401 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001402 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001403 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001404}
1405
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001406//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001407// AtomicCmpXchgInst Implementation
1408//===----------------------------------------------------------------------===//
1409
1410void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001411 AtomicOrdering SuccessOrdering,
1412 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001413 SynchronizationScope SynchScope) {
1414 Op<0>() = Ptr;
1415 Op<1>() = Cmp;
1416 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001417 setSuccessOrdering(SuccessOrdering);
1418 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001419 setSynchScope(SynchScope);
1420
1421 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1422 "All operands must be non-null!");
1423 assert(getOperand(0)->getType()->isPointerTy() &&
1424 "Ptr must have pointer type!");
1425 assert(getOperand(1)->getType() ==
1426 cast<PointerType>(getOperand(0)->getType())->getElementType()
1427 && "Ptr must be a pointer to Cmp type!");
1428 assert(getOperand(2)->getType() ==
1429 cast<PointerType>(getOperand(0)->getType())->getElementType()
1430 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001431 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001432 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001433 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001434 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001435 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1436 "AtomicCmpXchg failure argument shall be no stronger than the success "
1437 "argument");
1438 assert(FailureOrdering != AtomicOrdering::Release &&
1439 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001440 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001441}
1442
1443AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001444 AtomicOrdering SuccessOrdering,
1445 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001446 SynchronizationScope SynchScope,
1447 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001448 : Instruction(
1449 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1450 nullptr),
1451 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1452 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001453 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001454}
1455
1456AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001457 AtomicOrdering SuccessOrdering,
1458 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001459 SynchronizationScope SynchScope,
1460 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001461 : Instruction(
1462 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1463 nullptr),
1464 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1465 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001466 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001467}
Tim Northover420a2162014-06-13 14:24:07 +00001468
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001469//===----------------------------------------------------------------------===//
1470// AtomicRMWInst Implementation
1471//===----------------------------------------------------------------------===//
1472
1473void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1474 AtomicOrdering Ordering,
1475 SynchronizationScope SynchScope) {
1476 Op<0>() = Ptr;
1477 Op<1>() = Val;
1478 setOperation(Operation);
1479 setOrdering(Ordering);
1480 setSynchScope(SynchScope);
1481
1482 assert(getOperand(0) && getOperand(1) &&
1483 "All operands must be non-null!");
1484 assert(getOperand(0)->getType()->isPointerTy() &&
1485 "Ptr must have pointer type!");
1486 assert(getOperand(1)->getType() ==
1487 cast<PointerType>(getOperand(0)->getType())->getElementType()
1488 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001489 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001490 "AtomicRMW instructions must be atomic!");
1491}
1492
1493AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1494 AtomicOrdering Ordering,
1495 SynchronizationScope SynchScope,
1496 Instruction *InsertBefore)
1497 : Instruction(Val->getType(), AtomicRMW,
1498 OperandTraits<AtomicRMWInst>::op_begin(this),
1499 OperandTraits<AtomicRMWInst>::operands(this),
1500 InsertBefore) {
1501 Init(Operation, Ptr, Val, Ordering, SynchScope);
1502}
1503
1504AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1505 AtomicOrdering Ordering,
1506 SynchronizationScope SynchScope,
1507 BasicBlock *InsertAtEnd)
1508 : Instruction(Val->getType(), AtomicRMW,
1509 OperandTraits<AtomicRMWInst>::op_begin(this),
1510 OperandTraits<AtomicRMWInst>::operands(this),
1511 InsertAtEnd) {
1512 Init(Operation, Ptr, Val, Ordering, SynchScope);
1513}
1514
1515//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001516// FenceInst Implementation
1517//===----------------------------------------------------------------------===//
1518
1519FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1520 SynchronizationScope SynchScope,
1521 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001522 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001523 setOrdering(Ordering);
1524 setSynchScope(SynchScope);
1525}
1526
1527FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1528 SynchronizationScope SynchScope,
1529 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001530 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001531 setOrdering(Ordering);
1532 setSynchScope(SynchScope);
1533}
1534
1535//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001536// GetElementPtrInst Implementation
1537//===----------------------------------------------------------------------===//
1538
Pete Cooper1d078692015-12-14 20:29:16 +00001539void GetElementPtrInst::anchor() {}
1540
Jay Foadd1b78492011-07-25 09:48:08 +00001541void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001542 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001543 assert(getNumOperands() == 1 + IdxList.size() &&
1544 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001545 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001546 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001547 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001548}
1549
Gabor Greiff6caff662008-05-10 08:32:32 +00001550GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001551 : Instruction(GEPI.getType(), GetElementPtr,
1552 OperandTraits<GetElementPtrInst>::op_end(this) -
1553 GEPI.getNumOperands(),
1554 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001555 SourceElementType(GEPI.SourceElementType),
1556 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001557 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001558 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001559}
1560
Chris Lattner6090a422009-03-09 04:46:40 +00001561/// getIndexedType - Returns the type of the element that would be accessed with
1562/// a gep instruction with the specified parameters.
1563///
1564/// The Idxs pointer should point to a continuous piece of memory containing the
1565/// indices, either as Value* or uint64_t.
1566///
1567/// A null type is returned if the indices are invalid for the specified
1568/// pointer type.
1569///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001570template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001571static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001572 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001573 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001574 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001575
Chris Lattner6090a422009-03-09 04:46:40 +00001576 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001577 // it cannot be 'stepped over'.
1578 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001579 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001580
Dan Gohman1ecaf452008-05-31 00:58:22 +00001581 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001582 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001583 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001584 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001585 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001586 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001587 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001588 }
Craig Topperc6207612014-04-09 06:08:46 +00001589 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001590}
1591
David Blaikied288fb82015-03-30 21:41:43 +00001592Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1593 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001594}
1595
David Blaikied288fb82015-03-30 21:41:43 +00001596Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001597 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001598 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001599}
1600
David Blaikied288fb82015-03-30 21:41:43 +00001601Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1602 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001603}
1604
Chris Lattner45f15572007-04-14 00:12:57 +00001605/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1606/// zeros. If so, the result pointer and the first operand have the same
1607/// value, just potentially different types.
1608bool GetElementPtrInst::hasAllZeroIndices() const {
1609 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1610 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1611 if (!CI->isZero()) return false;
1612 } else {
1613 return false;
1614 }
1615 }
1616 return true;
1617}
1618
Chris Lattner27058292007-04-27 20:35:56 +00001619/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1620/// constant integers. If so, the result pointer and the first operand have
1621/// a constant offset between them.
1622bool GetElementPtrInst::hasAllConstantIndices() const {
1623 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1624 if (!isa<ConstantInt>(getOperand(i)))
1625 return false;
1626 }
1627 return true;
1628}
1629
Dan Gohman1b849082009-09-07 23:54:19 +00001630void GetElementPtrInst::setIsInBounds(bool B) {
1631 cast<GEPOperator>(this)->setIsInBounds(B);
1632}
Chris Lattner45f15572007-04-14 00:12:57 +00001633
Nick Lewycky28a5f252009-09-27 21:33:04 +00001634bool GetElementPtrInst::isInBounds() const {
1635 return cast<GEPOperator>(this)->isInBounds();
1636}
1637
Chandler Carruth1e140532012-12-11 10:29:10 +00001638bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1639 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001640 // Delegate to the generic GEPOperator implementation.
1641 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001642}
1643
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001644//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001645// ExtractElementInst Implementation
1646//===----------------------------------------------------------------------===//
1647
1648ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001649 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001650 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001651 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001652 ExtractElement,
1653 OperandTraits<ExtractElementInst>::op_begin(this),
1654 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001655 assert(isValidOperands(Val, Index) &&
1656 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001657 Op<0>() = Val;
1658 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001659 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001660}
1661
1662ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001663 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001664 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001665 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001666 ExtractElement,
1667 OperandTraits<ExtractElementInst>::op_begin(this),
1668 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001669 assert(isValidOperands(Val, Index) &&
1670 "Invalid extractelement instruction operands!");
1671
Gabor Greif2d3024d2008-05-26 21:33:52 +00001672 Op<0>() = Val;
1673 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001674 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001675}
1676
Chris Lattner65511ff2006-10-05 06:24:58 +00001677
Chris Lattner54865b32006-04-08 04:05:48 +00001678bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001679 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001680 return false;
1681 return true;
1682}
1683
1684
Robert Bocchino23004482006-01-10 19:05:34 +00001685//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001686// InsertElementInst Implementation
1687//===----------------------------------------------------------------------===//
1688
Chris Lattner54865b32006-04-08 04:05:48 +00001689InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001690 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001691 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001692 : Instruction(Vec->getType(), InsertElement,
1693 OperandTraits<InsertElementInst>::op_begin(this),
1694 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001695 assert(isValidOperands(Vec, Elt, Index) &&
1696 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001697 Op<0>() = Vec;
1698 Op<1>() = Elt;
1699 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001700 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001701}
1702
Chris Lattner54865b32006-04-08 04:05:48 +00001703InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001704 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001705 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001706 : Instruction(Vec->getType(), InsertElement,
1707 OperandTraits<InsertElementInst>::op_begin(this),
1708 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001709 assert(isValidOperands(Vec, Elt, Index) &&
1710 "Invalid insertelement instruction operands!");
1711
Gabor Greif2d3024d2008-05-26 21:33:52 +00001712 Op<0>() = Vec;
1713 Op<1>() = Elt;
1714 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001715 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001716}
1717
Chris Lattner54865b32006-04-08 04:05:48 +00001718bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1719 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001720 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001721 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001722
Reid Spencerd84d35b2007-02-15 02:26:10 +00001723 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001724 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001725
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001726 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001727 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001728 return true;
1729}
1730
1731
Robert Bocchinoca27f032006-01-17 20:07:22 +00001732//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001733// ShuffleVectorInst Implementation
1734//===----------------------------------------------------------------------===//
1735
1736ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001737 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001738 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001739: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001740 cast<VectorType>(Mask->getType())->getNumElements()),
1741 ShuffleVector,
1742 OperandTraits<ShuffleVectorInst>::op_begin(this),
1743 OperandTraits<ShuffleVectorInst>::operands(this),
1744 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001745 assert(isValidOperands(V1, V2, Mask) &&
1746 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001747 Op<0>() = V1;
1748 Op<1>() = V2;
1749 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001750 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001751}
1752
1753ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001754 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001755 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001756: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1757 cast<VectorType>(Mask->getType())->getNumElements()),
1758 ShuffleVector,
1759 OperandTraits<ShuffleVectorInst>::op_begin(this),
1760 OperandTraits<ShuffleVectorInst>::operands(this),
1761 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001762 assert(isValidOperands(V1, V2, Mask) &&
1763 "Invalid shuffle vector instruction operands!");
1764
Gabor Greif2d3024d2008-05-26 21:33:52 +00001765 Op<0>() = V1;
1766 Op<1>() = V2;
1767 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001768 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001769}
1770
Mon P Wang25f01062008-11-10 04:46:22 +00001771bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001772 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001773 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001774 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001775 return false;
1776
Chris Lattner1dcb6542012-01-25 23:49:49 +00001777 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001778 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001779 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001780 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001781
1782 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001783 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1784 return true;
1785
Nate Begeman60a31c32010-08-13 00:16:46 +00001786 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001787 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001788 for (Value *Op : MV->operands()) {
1789 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001790 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001791 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001792 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001793 return false;
1794 }
1795 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001796 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001797 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001798
1799 if (const ConstantDataSequential *CDS =
1800 dyn_cast<ConstantDataSequential>(Mask)) {
1801 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1802 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1803 if (CDS->getElementAsInteger(i) >= V1Size*2)
1804 return false;
1805 return true;
1806 }
1807
1808 // The bitcode reader can create a place holder for a forward reference
1809 // used as the shuffle mask. When this occurs, the shuffle mask will
1810 // fall into this case and fail. To avoid this error, do this bit of
1811 // ugliness to allow such a mask pass.
1812 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1813 if (CE->getOpcode() == Instruction::UserOp1)
1814 return true;
1815
1816 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001817}
1818
Chris Lattnerf724e342008-03-02 05:28:33 +00001819/// getMaskValue - Return the index from the shuffle mask for the specified
1820/// output result. This is either -1 if the element is undef or a number less
1821/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001822int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1823 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1824 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001825 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001826 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001827 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001828 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001829 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001830}
1831
Chris Lattner1dcb6542012-01-25 23:49:49 +00001832/// getShuffleMask - Return the full mask for this instruction, where each
1833/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001834void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1835 SmallVectorImpl<int> &Result) {
1836 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001837
Chris Lattnercf129702012-01-26 02:51:13 +00001838 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001839 for (unsigned i = 0; i != NumElts; ++i)
1840 Result.push_back(CDS->getElementAsInteger(i));
1841 return;
1842 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001843 for (unsigned i = 0; i != NumElts; ++i) {
1844 Constant *C = Mask->getAggregateElement(i);
1845 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001846 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001847 }
1848}
1849
1850
Dan Gohman12fce772008-05-15 19:50:34 +00001851//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001852// InsertValueInst Class
1853//===----------------------------------------------------------------------===//
1854
Jay Foad57aa6362011-07-13 10:26:04 +00001855void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1856 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001857 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001858
1859 // There's no fundamental reason why we require at least one index
1860 // (other than weirdness with &*IdxBegin being invalid; see
1861 // getelementptr's init routine for example). But there's no
1862 // present need to support it.
1863 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1864
1865 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001866 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001867 Op<0>() = Agg;
1868 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001869
Jay Foad57aa6362011-07-13 10:26:04 +00001870 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001871 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001872}
1873
1874InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001875 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001876 OperandTraits<InsertValueInst>::op_begin(this), 2),
1877 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001878 Op<0>() = IVI.getOperand(0);
1879 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001880 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001881}
1882
1883//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001884// ExtractValueInst Class
1885//===----------------------------------------------------------------------===//
1886
Jay Foad57aa6362011-07-13 10:26:04 +00001887void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001888 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001889
Jay Foad57aa6362011-07-13 10:26:04 +00001890 // There's no fundamental reason why we require at least one index.
1891 // But there's no present need to support it.
1892 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001893
Jay Foad57aa6362011-07-13 10:26:04 +00001894 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001895 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001896}
1897
1898ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001899 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001900 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001901 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001902}
1903
Dan Gohman12fce772008-05-15 19:50:34 +00001904// getIndexedType - Returns the type of the element that would be extracted
1905// with an extractvalue instruction with the specified parameters.
1906//
1907// A null type is returned if the indices are invalid for the specified
1908// pointer type.
1909//
Chris Lattner229907c2011-07-18 04:54:35 +00001910Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001911 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001912 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001913 // We can't use CompositeType::indexValid(Index) here.
1914 // indexValid() always returns true for arrays because getelementptr allows
1915 // out-of-bounds indices. Since we don't allow those for extractvalue and
1916 // insertvalue we need to check array indexing manually.
1917 // Since the only other types we can index into are struct types it's just
1918 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001919 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001920 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001921 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001922 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001923 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001924 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001925 } else {
1926 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001927 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001928 }
1929
1930 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001931 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001932 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001933}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001934
1935//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001936// BinaryOperator Class
1937//===----------------------------------------------------------------------===//
1938
Chris Lattner2195fc42007-02-24 00:55:48 +00001939BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001940 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001941 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001942 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001943 OperandTraits<BinaryOperator>::op_begin(this),
1944 OperandTraits<BinaryOperator>::operands(this),
1945 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001946 Op<0>() = S1;
1947 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001948 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001949 setName(Name);
1950}
1951
1952BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001953 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001954 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001955 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001956 OperandTraits<BinaryOperator>::op_begin(this),
1957 OperandTraits<BinaryOperator>::operands(this),
1958 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001959 Op<0>() = S1;
1960 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001961 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001962 setName(Name);
1963}
1964
1965
1966void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001967 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001968 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001969 assert(LHS->getType() == RHS->getType() &&
1970 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001971#ifndef NDEBUG
1972 switch (iType) {
1973 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001974 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001975 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001976 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001977 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001978 "Tried to create an integer operation on a non-integer type!");
1979 break;
1980 case FAdd: case FSub:
1981 case FMul:
1982 assert(getType() == LHS->getType() &&
1983 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001984 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001985 "Tried to create a floating-point operation on a "
1986 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001987 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001988 case UDiv:
1989 case SDiv:
1990 assert(getType() == LHS->getType() &&
1991 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001992 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001993 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001994 "Incorrect operand type (not integer) for S/UDIV");
1995 break;
1996 case FDiv:
1997 assert(getType() == LHS->getType() &&
1998 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001999 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002000 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00002001 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00002002 case URem:
2003 case SRem:
2004 assert(getType() == LHS->getType() &&
2005 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00002006 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002007 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00002008 "Incorrect operand type (not integer) for S/UREM");
2009 break;
2010 case FRem:
2011 assert(getType() == LHS->getType() &&
2012 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002013 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002014 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00002015 break;
Reid Spencer2341c222007-02-02 02:16:23 +00002016 case Shl:
2017 case LShr:
2018 case AShr:
2019 assert(getType() == LHS->getType() &&
2020 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002021 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002022 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002023 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00002024 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00002025 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002026 case And: case Or:
2027 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002028 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002029 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002030 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002031 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002032 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002033 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002034 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002035 default:
2036 break;
2037 }
2038#endif
2039}
2040
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002041BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002042 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002043 Instruction *InsertBefore) {
2044 assert(S1->getType() == S2->getType() &&
2045 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002046 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002047}
2048
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002049BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002050 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002051 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002052 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002053 InsertAtEnd->getInstList().push_back(Res);
2054 return Res;
2055}
2056
Dan Gohman5476cfd2009-08-12 16:23:25 +00002057BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002058 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002059 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002060 return new BinaryOperator(Instruction::Sub,
2061 zero, Op,
2062 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002063}
2064
Dan Gohman5476cfd2009-08-12 16:23:25 +00002065BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002066 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002067 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002068 return new BinaryOperator(Instruction::Sub,
2069 zero, Op,
2070 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002071}
2072
Dan Gohman4ab44202009-12-18 02:58:50 +00002073BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2074 Instruction *InsertBefore) {
2075 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2076 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2077}
2078
2079BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2080 BasicBlock *InsertAtEnd) {
2081 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2082 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2083}
2084
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002085BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2086 Instruction *InsertBefore) {
2087 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2088 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2089}
2090
2091BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2092 BasicBlock *InsertAtEnd) {
2093 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2094 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2095}
2096
Dan Gohman5476cfd2009-08-12 16:23:25 +00002097BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002098 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002099 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002100 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002101 Op->getType(), Name, InsertBefore);
2102}
2103
Dan Gohman5476cfd2009-08-12 16:23:25 +00002104BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002105 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002106 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002107 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002108 Op->getType(), Name, InsertAtEnd);
2109}
2110
Dan Gohman5476cfd2009-08-12 16:23:25 +00002111BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002112 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002113 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002114 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002115 Op->getType(), Name, InsertBefore);
2116}
2117
Dan Gohman5476cfd2009-08-12 16:23:25 +00002118BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002119 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002120 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002121 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002122 Op->getType(), Name, InsertAtEnd);
2123}
2124
2125
2126// isConstantAllOnes - Helper function for several functions below
2127static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002128 if (const Constant *C = dyn_cast<Constant>(V))
2129 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002130 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002131}
2132
Owen Andersonbb2501b2009-07-13 22:18:28 +00002133bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002134 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2135 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00002136 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00002137 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002138 return false;
2139}
2140
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002141bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002142 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2143 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00002144 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002145 if (!IgnoreZeroSign)
2146 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2147 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2148 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002149 return false;
2150}
2151
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002152bool BinaryOperator::isNot(const Value *V) {
2153 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2154 return (Bop->getOpcode() == Instruction::Xor &&
2155 (isConstantAllOnes(Bop->getOperand(1)) ||
2156 isConstantAllOnes(Bop->getOperand(0))));
2157 return false;
2158}
2159
Chris Lattner2c7d1772005-04-24 07:28:37 +00002160Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002161 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002162}
2163
Chris Lattner2c7d1772005-04-24 07:28:37 +00002164const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2165 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002166}
2167
Dan Gohmana5b96452009-06-04 22:49:04 +00002168Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002169 return cast<BinaryOperator>(BinOp)->getOperand(1);
2170}
2171
2172const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2173 return getFNegArgument(const_cast<Value*>(BinOp));
2174}
2175
Chris Lattner2c7d1772005-04-24 07:28:37 +00002176Value *BinaryOperator::getNotArgument(Value *BinOp) {
2177 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2178 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2179 Value *Op0 = BO->getOperand(0);
2180 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002181 if (isConstantAllOnes(Op0)) return Op1;
2182
2183 assert(isConstantAllOnes(Op1));
2184 return Op0;
2185}
2186
Chris Lattner2c7d1772005-04-24 07:28:37 +00002187const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2188 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002189}
2190
2191
2192// swapOperands - Exchange the two operands to this instruction. This
2193// instruction is safe to use on any binary instruction and does not
2194// modify the semantics of the instruction. If the instruction is
2195// order dependent (SetLT f.e.) the opcode is changed.
2196//
2197bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002198 if (!isCommutative())
2199 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002200 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002201 return false;
2202}
2203
Sanjay Patela982d992014-09-03 01:06:50 +00002204
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002205//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002206// FPMathOperator Class
2207//===----------------------------------------------------------------------===//
2208
2209/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2210/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002211/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002212float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002213 const MDNode *MD =
2214 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002215 if (!MD)
2216 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002217 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002218 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002219}
2220
2221
2222//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002223// CastInst Class
2224//===----------------------------------------------------------------------===//
2225
David Blaikie3a15e142011-12-01 08:00:17 +00002226void CastInst::anchor() {}
2227
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002228// Just determine if this cast only deals with integral->integral conversion.
2229bool CastInst::isIntegerCast() const {
2230 switch (getOpcode()) {
2231 default: return false;
2232 case Instruction::ZExt:
2233 case Instruction::SExt:
2234 case Instruction::Trunc:
2235 return true;
2236 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002237 return getOperand(0)->getType()->isIntegerTy() &&
2238 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002239 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002240}
2241
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002242bool CastInst::isLosslessCast() const {
2243 // Only BitCast can be lossless, exit fast if we're not BitCast
2244 if (getOpcode() != Instruction::BitCast)
2245 return false;
2246
2247 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002248 Type *SrcTy = getOperand(0)->getType();
2249 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250 if (SrcTy == DstTy)
2251 return true;
2252
Reid Spencer8d9336d2006-12-31 05:26:44 +00002253 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002254 if (SrcTy->isPointerTy())
2255 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256 return false; // Other types have no identity values
2257}
2258
2259/// This function determines if the CastInst does not require any bits to be
2260/// changed in order to effect the cast. Essentially, it identifies cases where
2261/// no code gen is necessary for the cast, hence the name no-op cast. For
2262/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002263/// # bitcast i32* %x to i8*
2264/// # bitcast <2 x i32> %x to <4 x i16>
2265/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002266/// @brief Determine if the described cast is a no-op.
2267bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002268 Type *SrcTy,
2269 Type *DestTy,
2270 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002271 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002272 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002273 case Instruction::Trunc:
2274 case Instruction::ZExt:
2275 case Instruction::SExt:
2276 case Instruction::FPTrunc:
2277 case Instruction::FPExt:
2278 case Instruction::UIToFP:
2279 case Instruction::SIToFP:
2280 case Instruction::FPToUI:
2281 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002282 case Instruction::AddrSpaceCast:
2283 // TODO: Target informations may give a more accurate answer here.
2284 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002285 case Instruction::BitCast:
2286 return true; // BitCast never modifies bits.
2287 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002288 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002289 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002290 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002291 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002292 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002293 }
2294}
2295
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002296/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002297bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002298 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2299}
2300
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002301bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002302 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002303 if (getOpcode() == Instruction::PtrToInt)
2304 PtrOpTy = getOperand(0)->getType();
2305 else if (getOpcode() == Instruction::IntToPtr)
2306 PtrOpTy = getType();
2307
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002308 Type *IntPtrTy =
2309 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002310
2311 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2312}
2313
2314/// This function determines if a pair of casts can be eliminated and what
2315/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002316/// instructions like this:
2317/// * %F = firstOpcode SrcTy %x to MidTy
2318/// * %S = secondOpcode MidTy %F to DstTy
2319/// The function returns a resultOpcode so these two casts can be replaced with:
2320/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002321/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002322unsigned CastInst::isEliminableCastPair(
2323 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002324 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2325 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002326 // Define the 144 possibilities for these two cast instructions. The values
2327 // in this matrix determine what to do in a given situation and select the
2328 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002329 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002330 // the following cast properties:
2331 //
2332 // Size Compare Source Destination
2333 // Operator Src ? Size Type Sign Type Sign
2334 // -------- ------------ ------------------- ---------------------
2335 // TRUNC > Integer Any Integral Any
2336 // ZEXT < Integral Unsigned Integer Any
2337 // SEXT < Integral Signed Integer Any
2338 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002339 // FPTOSI n/a FloatPt n/a Integral Signed
2340 // UITOFP n/a Integral Unsigned FloatPt n/a
2341 // SITOFP n/a Integral Signed FloatPt n/a
2342 // FPTRUNC > FloatPt n/a FloatPt n/a
2343 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344 // PTRTOINT n/a Pointer n/a Integral Unsigned
2345 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002346 // BITCAST = FirstClass n/a FirstClass n/a
2347 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002348 //
2349 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002350 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2351 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002352 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002353 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002354 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002355 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002356 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2358 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002359 // T F F U S F F P I B A -+
2360 // R Z S P P I I T P 2 N T S |
2361 // U E E 2 2 2 2 R E I T C C +- secondOp
2362 // N X X U S F F N X N 2 V V |
2363 // C T T I I P P C T T P T T -+
2364 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002365 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002366 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2367 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2368 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2369 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2370 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002371 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002372 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2373 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2374 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2375 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2376 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002378
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002379 // TODO: This logic could be encoded into the table above and handled in the
2380 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002381 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002382 // merging. However, any pair of bitcasts are allowed.
2383 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2384 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2385 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002386
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002387 // Check if any of the casts convert scalars <-> vectors.
2388 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2389 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2390 if (!AreBothBitcasts)
2391 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002392
2393 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2394 [secondOp-Instruction::CastOpsBegin];
2395 switch (ElimCase) {
2396 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002397 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002398 return 0;
2399 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002400 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 return firstOp;
2402 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002403 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002404 return secondOp;
2405 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002406 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002407 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002408 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002409 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 return firstOp;
2411 return 0;
2412 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002413 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002414 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002415 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002416 return firstOp;
2417 return 0;
2418 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002419 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002420 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002421 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 return secondOp;
2423 return 0;
2424 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002425 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002426 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002427 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002428 return secondOp;
2429 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002430 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002431 // Cannot simplify if address spaces are different!
2432 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2433 return 0;
2434
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002435 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002436 // We can still fold this without knowing the actual sizes as long we
2437 // know that the intermediate pointer is the largest possible
2438 // pointer size.
2439 // FIXME: Is this always true?
2440 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002441 return Instruction::BitCast;
2442
2443 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002444 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002445 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002446 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 if (MidSize >= PtrSize)
2448 return Instruction::BitCast;
2449 return 0;
2450 }
2451 case 8: {
2452 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2453 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2454 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002455 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2456 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002457 if (SrcSize == DstSize)
2458 return Instruction::BitCast;
2459 else if (SrcSize < DstSize)
2460 return firstOp;
2461 return secondOp;
2462 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002463 case 9:
2464 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002465 return Instruction::ZExt;
2466 case 10:
2467 // fpext followed by ftrunc is allowed if the bit size returned to is
2468 // the same as the original, in which case its just a bitcast
2469 if (SrcTy == DstTy)
2470 return Instruction::BitCast;
2471 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002472 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002473 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002474 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002475 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002476 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002477 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2478 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002479 if (SrcSize <= PtrSize && SrcSize == DstSize)
2480 return Instruction::BitCast;
2481 return 0;
2482 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002483 case 12: {
2484 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2485 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2486 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2487 return Instruction::AddrSpaceCast;
2488 return Instruction::BitCast;
2489 }
2490 case 13:
2491 // FIXME: this state can be merged with (1), but the following assert
2492 // is useful to check the correcteness of the sequence due to semantic
2493 // change of bitcast.
2494 assert(
2495 SrcTy->isPtrOrPtrVectorTy() &&
2496 MidTy->isPtrOrPtrVectorTy() &&
2497 DstTy->isPtrOrPtrVectorTy() &&
2498 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2499 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2500 "Illegal addrspacecast, bitcast sequence!");
2501 // Allowed, use first cast's opcode
2502 return firstOp;
2503 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002504 // bitcast, addrspacecast -> addrspacecast if the element type of
2505 // bitcast's source is the same as that of addrspacecast's destination.
2506 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2507 return Instruction::AddrSpaceCast;
2508 return 0;
2509
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002510 case 15:
2511 // FIXME: this state can be merged with (1), but the following assert
2512 // is useful to check the correcteness of the sequence due to semantic
2513 // change of bitcast.
2514 assert(
2515 SrcTy->isIntOrIntVectorTy() &&
2516 MidTy->isPtrOrPtrVectorTy() &&
2517 DstTy->isPtrOrPtrVectorTy() &&
2518 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2519 "Illegal inttoptr, bitcast sequence!");
2520 // Allowed, use first cast's opcode
2521 return firstOp;
2522 case 16:
2523 // FIXME: this state can be merged with (2), but the following assert
2524 // is useful to check the correcteness of the sequence due to semantic
2525 // change of bitcast.
2526 assert(
2527 SrcTy->isPtrOrPtrVectorTy() &&
2528 MidTy->isPtrOrPtrVectorTy() &&
2529 DstTy->isIntOrIntVectorTy() &&
2530 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2531 "Illegal bitcast, ptrtoint sequence!");
2532 // Allowed, use second cast's opcode
2533 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002534 case 17:
2535 // (sitofp (zext x)) -> (uitofp x)
2536 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002538 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002540 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541 default:
Craig Topperc514b542012-02-05 22:14:15 +00002542 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002543 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544}
2545
Chris Lattner229907c2011-07-18 04:54:35 +00002546CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002547 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002548 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002549 // Construct and return the appropriate CastInst subclass
2550 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002551 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2552 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2553 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2554 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2555 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2556 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2557 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2558 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2559 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2560 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2561 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2562 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2563 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2564 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566}
2567
Chris Lattner229907c2011-07-18 04:54:35 +00002568CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002569 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002570 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002571 // Construct and return the appropriate CastInst subclass
2572 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002573 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2574 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2575 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2576 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2577 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2578 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2579 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2580 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2581 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2582 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2583 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2584 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2585 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2586 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
Chris Lattner229907c2011-07-18 04:54:35 +00002590CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002592 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002593 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002594 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2595 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002596}
2597
Chris Lattner229907c2011-07-18 04:54:35 +00002598CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002599 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002600 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002601 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002602 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2603 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002604}
2605
Chris Lattner229907c2011-07-18 04:54:35 +00002606CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002607 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002608 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002609 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002610 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2611 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002612}
2613
Chris Lattner229907c2011-07-18 04:54:35 +00002614CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002616 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002617 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002618 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2619 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002620}
2621
Chris Lattner229907c2011-07-18 04:54:35 +00002622CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002623 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002624 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002625 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002626 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2627 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002628}
2629
Chris Lattner229907c2011-07-18 04:54:35 +00002630CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002631 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002632 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002633 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002634 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2635 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002636}
2637
Chris Lattner229907c2011-07-18 04:54:35 +00002638CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002639 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002640 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002641 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2642 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2643 "Invalid cast");
2644 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002645 assert((!Ty->isVectorTy() ||
2646 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002647 "Invalid cast");
2648
Matt Arsenault065ced92013-07-31 00:17:33 +00002649 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002650 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002651
Matt Arsenault740980e2014-07-14 17:24:35 +00002652 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002653}
2654
2655/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002656CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2657 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002658 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002659 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2660 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002661 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002662 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002663 assert((!Ty->isVectorTy() ||
2664 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002665 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002666
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002667 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002668 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002669
Matt Arsenault740980e2014-07-14 17:24:35 +00002670 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2671}
2672
2673CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2674 Value *S, Type *Ty,
2675 const Twine &Name,
2676 BasicBlock *InsertAtEnd) {
2677 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2678 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2679
2680 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2681 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2682
2683 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2684}
2685
2686CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2687 Value *S, Type *Ty,
2688 const Twine &Name,
2689 Instruction *InsertBefore) {
2690 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2691 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2692
2693 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002694 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2695
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002696 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002697}
2698
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002699CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2700 const Twine &Name,
2701 Instruction *InsertBefore) {
2702 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2703 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2704 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2705 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2706
2707 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2708}
2709
Matt Arsenault740980e2014-07-14 17:24:35 +00002710CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002711 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002712 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002713 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002714 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002715 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2716 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002717 Instruction::CastOps opcode =
2718 (SrcBits == DstBits ? Instruction::BitCast :
2719 (SrcBits > DstBits ? Instruction::Trunc :
2720 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002721 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002722}
2723
Chris Lattner229907c2011-07-18 04:54:35 +00002724CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002725 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002726 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002727 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002728 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002729 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2730 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002731 Instruction::CastOps opcode =
2732 (SrcBits == DstBits ? Instruction::BitCast :
2733 (SrcBits > DstBits ? Instruction::Trunc :
2734 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002735 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002736}
2737
Chris Lattner229907c2011-07-18 04:54:35 +00002738CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002739 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002740 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002741 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002742 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002743 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2744 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002745 Instruction::CastOps opcode =
2746 (SrcBits == DstBits ? Instruction::BitCast :
2747 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002748 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002749}
2750
Chris Lattner229907c2011-07-18 04:54:35 +00002751CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002752 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002753 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002754 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002755 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002756 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2757 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002758 Instruction::CastOps opcode =
2759 (SrcBits == DstBits ? Instruction::BitCast :
2760 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002761 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002762}
2763
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002764// Check whether it is valid to call getCastOpcode for these types.
2765// This routine must be kept in sync with getCastOpcode.
2766bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2767 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2768 return false;
2769
2770 if (SrcTy == DestTy)
2771 return true;
2772
2773 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2774 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2775 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2776 // An element by element cast. Valid if casting the elements is valid.
2777 SrcTy = SrcVecTy->getElementType();
2778 DestTy = DestVecTy->getElementType();
2779 }
2780
2781 // Get the bit sizes, we'll need these
2782 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2783 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2784
2785 // Run through the possibilities ...
2786 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002787 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002788 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002789 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002790 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002791 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002792 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002793 // Casting from something else
2794 return SrcTy->isPointerTy();
2795 }
2796 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2797 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002798 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002799 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002800 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002801 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002802 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002803 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002804 return false;
2805 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002806 if (DestTy->isVectorTy()) // Casting to vector
2807 return DestBits == SrcBits;
2808 if (DestTy->isPointerTy()) { // Casting to pointer
2809 if (SrcTy->isPointerTy()) // Casting from pointer
2810 return true;
2811 return SrcTy->isIntegerTy(); // Casting from integral
2812 }
2813 if (DestTy->isX86_MMXTy()) {
2814 if (SrcTy->isVectorTy())
2815 return DestBits == SrcBits; // 64-bit vector to MMX
2816 return false;
2817 } // Casting to something else
2818 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002819}
2820
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002821bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2822 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2823 return false;
2824
2825 if (SrcTy == DestTy)
2826 return true;
2827
2828 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2829 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2830 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2831 // An element by element cast. Valid if casting the elements is valid.
2832 SrcTy = SrcVecTy->getElementType();
2833 DestTy = DestVecTy->getElementType();
2834 }
2835 }
2836 }
2837
2838 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2839 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2840 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2841 }
2842 }
2843
2844 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2845 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2846
2847 // Could still have vectors of pointers if the number of elements doesn't
2848 // match
2849 if (SrcBits == 0 || DestBits == 0)
2850 return false;
2851
2852 if (SrcBits != DestBits)
2853 return false;
2854
2855 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2856 return false;
2857
2858 return true;
2859}
2860
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002861bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002862 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002863 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2864 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002865 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002866 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2867 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002868 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002869
2870 return isBitCastable(SrcTy, DestTy);
2871}
2872
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002873// Provide a way to get a "cast" where the cast opcode is inferred from the
2874// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002875// logic in the castIsValid function below. This axiom should hold:
2876// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2877// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002878// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002879// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002880Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002881CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002882 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2883 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002884
Duncan Sands55e50902008-01-06 10:12:28 +00002885 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2886 "Only first class types are castable!");
2887
Duncan Sandsa8514532011-05-18 07:13:41 +00002888 if (SrcTy == DestTy)
2889 return BitCast;
2890
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002891 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002892 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2893 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002894 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2895 // An element by element cast. Find the appropriate opcode based on the
2896 // element types.
2897 SrcTy = SrcVecTy->getElementType();
2898 DestTy = DestVecTy->getElementType();
2899 }
2900
2901 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002902 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2903 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002904
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002906 if (DestTy->isIntegerTy()) { // Casting to integral
2907 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002908 if (DestBits < SrcBits)
2909 return Trunc; // int -> smaller int
2910 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002911 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002912 return SExt; // signed -> SEXT
2913 else
2914 return ZExt; // unsigned -> ZEXT
2915 } else {
2916 return BitCast; // Same size, No-op cast
2917 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002918 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002919 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002920 return FPToSI; // FP -> sint
2921 else
2922 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002923 } else if (SrcTy->isVectorTy()) {
2924 assert(DestBits == SrcBits &&
2925 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002926 return BitCast; // Same size, no-op cast
2927 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002928 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929 "Casting from a value that is not first-class type");
2930 return PtrToInt; // ptr -> int
2931 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002932 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2933 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002934 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002935 return SIToFP; // sint -> FP
2936 else
2937 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002938 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002939 if (DestBits < SrcBits) {
2940 return FPTrunc; // FP -> smaller FP
2941 } else if (DestBits > SrcBits) {
2942 return FPExt; // FP -> larger FP
2943 } else {
2944 return BitCast; // same size, no-op cast
2945 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002946 } else if (SrcTy->isVectorTy()) {
2947 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002948 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002949 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002950 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002951 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002952 } else if (DestTy->isVectorTy()) {
2953 assert(DestBits == SrcBits &&
2954 "Illegal cast to vector (wrong type or size)");
2955 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002956 } else if (DestTy->isPointerTy()) {
2957 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002958 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2959 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002960 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002961 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002962 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002963 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002964 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002965 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002966 if (SrcTy->isVectorTy()) {
2967 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002968 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002969 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002970 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002972 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002973}
2974
2975//===----------------------------------------------------------------------===//
2976// CastInst SubClass Constructors
2977//===----------------------------------------------------------------------===//
2978
2979/// Check that the construction parameters for a CastInst are correct. This
2980/// could be broken out into the separate constructors but it is useful to have
2981/// it in one place and to eliminate the redundant code for getting the sizes
2982/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002983bool
Chris Lattner229907c2011-07-18 04:54:35 +00002984CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002985
2986 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002987 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002988
Chris Lattner37bc78a2010-01-26 21:51:43 +00002989 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2990 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002991 return false;
2992
2993 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002994 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2995 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002996
Duncan Sands7f646562011-05-18 09:21:57 +00002997 // If these are vector types, get the lengths of the vectors (using zero for
2998 // scalar types means that checking that vector lengths match also checks that
2999 // scalars are not being converted to vectors or vectors to scalars).
3000 unsigned SrcLength = SrcTy->isVectorTy() ?
3001 cast<VectorType>(SrcTy)->getNumElements() : 0;
3002 unsigned DstLength = DstTy->isVectorTy() ?
3003 cast<VectorType>(DstTy)->getNumElements() : 0;
3004
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003005 // Switch on the opcode provided
3006 switch (op) {
3007 default: return false; // This is an input error
3008 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003009 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3010 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003012 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3013 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003014 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003015 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3016 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003018 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3019 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003020 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003021 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3022 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003024 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003025 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3026 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003027 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003028 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003029 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3030 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003031 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003032 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003033 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003034 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3035 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3036 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003037 return SrcTy->getScalarType()->isPointerTy() &&
3038 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003039 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003040 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003041 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003042 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3043 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3044 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003045 return SrcTy->getScalarType()->isIntegerTy() &&
3046 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003047 case Instruction::BitCast: {
3048 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3049 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3050
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003051 // BitCast implies a no-op cast of type only. No bits change.
3052 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003053 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003054 return false;
3055
Alp Tokerf907b892013-12-05 05:44:44 +00003056 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003057 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003058 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003059 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3060
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003061 // If both are pointers then the address spaces must match.
3062 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3063 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003064
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003065 // A vector of pointers must have the same number of elements.
3066 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3067 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3068 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3069
3070 return false;
3071 }
3072
3073 return true;
3074 }
3075 case Instruction::AddrSpaceCast: {
3076 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3077 if (!SrcPtrTy)
3078 return false;
3079
3080 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3081 if (!DstPtrTy)
3082 return false;
3083
3084 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3085 return false;
3086
3087 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3088 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3089 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3090
3091 return false;
3092 }
3093
3094 return true;
3095 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003096 }
3097}
3098
3099TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003100 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003101) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003102 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003103}
3104
3105TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003106 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003107) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003108 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003109}
3110
3111ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003112 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003113) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003114 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003115}
3116
3117ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003118 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003119) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003120 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003121}
3122SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003123 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003124) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003125 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003126}
3127
Jeff Cohencc08c832006-12-02 02:22:01 +00003128SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003129 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003130) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003131 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132}
3133
3134FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003135 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003136) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003137 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003138}
3139
3140FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003141 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003142) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003143 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003144}
3145
3146FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003147 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003148) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003149 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003150}
3151
3152FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003153 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003154) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003155 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003156}
3157
3158UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003159 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003160) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003161 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003162}
3163
3164UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003165 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003166) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003167 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003168}
3169
3170SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003171 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003172) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003173 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003174}
3175
3176SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003177 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003178) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003179 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003180}
3181
3182FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003183 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003184) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003185 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003186}
3187
3188FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003189 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003190) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003191 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003192}
3193
3194FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003195 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003196) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003197 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003198}
3199
3200FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003201 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003202) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003203 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003204}
3205
3206PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003207 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003208) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003209 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003210}
3211
3212PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003213 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003214) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003215 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003216}
3217
3218IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003219 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003220) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003221 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003222}
3223
3224IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003225 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003226) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003227 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003228}
3229
3230BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003231 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003232) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003233 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003234}
3235
3236BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003237 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003238) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003239 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003240}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003241
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003242AddrSpaceCastInst::AddrSpaceCastInst(
3243 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3244) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3245 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3246}
3247
3248AddrSpaceCastInst::AddrSpaceCastInst(
3249 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3250) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3251 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3252}
3253
Chris Lattnerf16dc002006-09-17 19:29:56 +00003254//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003255// CmpInst Classes
3256//===----------------------------------------------------------------------===//
3257
Craig Topper3186c012012-09-23 02:12:10 +00003258void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003259
Craig Topper1c3f2832015-12-15 06:11:33 +00003260CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3261 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003262 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003263 OperandTraits<CmpInst>::op_begin(this),
3264 OperandTraits<CmpInst>::operands(this),
3265 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003266 Op<0>() = LHS;
3267 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003268 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003269 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003270}
Gabor Greiff6caff662008-05-10 08:32:32 +00003271
Craig Topper1c3f2832015-12-15 06:11:33 +00003272CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3273 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003274 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003275 OperandTraits<CmpInst>::op_begin(this),
3276 OperandTraits<CmpInst>::operands(this),
3277 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003278 Op<0>() = LHS;
3279 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003280 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003281 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003282}
3283
3284CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003285CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003286 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003287 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003288 if (InsertBefore)
3289 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3290 S1, S2, Name);
3291 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003292 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003293 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003294 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003295
3296 if (InsertBefore)
3297 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3298 S1, S2, Name);
3299 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003300 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003301 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003302}
3303
3304CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003305CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003306 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003307 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003308 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3309 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003310 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003311 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3312 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003313}
3314
3315void CmpInst::swapOperands() {
3316 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3317 IC->swapOperands();
3318 else
3319 cast<FCmpInst>(this)->swapOperands();
3320}
3321
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003322bool CmpInst::isCommutative() const {
3323 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003324 return IC->isCommutative();
3325 return cast<FCmpInst>(this)->isCommutative();
3326}
3327
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003328bool CmpInst::isEquality() const {
3329 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003330 return IC->isEquality();
3331 return cast<FCmpInst>(this)->isEquality();
3332}
3333
3334
Dan Gohman4e724382008-05-31 02:47:54 +00003335CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003336 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003337 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003338 case ICMP_EQ: return ICMP_NE;
3339 case ICMP_NE: return ICMP_EQ;
3340 case ICMP_UGT: return ICMP_ULE;
3341 case ICMP_ULT: return ICMP_UGE;
3342 case ICMP_UGE: return ICMP_ULT;
3343 case ICMP_ULE: return ICMP_UGT;
3344 case ICMP_SGT: return ICMP_SLE;
3345 case ICMP_SLT: return ICMP_SGE;
3346 case ICMP_SGE: return ICMP_SLT;
3347 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003348
Dan Gohman4e724382008-05-31 02:47:54 +00003349 case FCMP_OEQ: return FCMP_UNE;
3350 case FCMP_ONE: return FCMP_UEQ;
3351 case FCMP_OGT: return FCMP_ULE;
3352 case FCMP_OLT: return FCMP_UGE;
3353 case FCMP_OGE: return FCMP_ULT;
3354 case FCMP_OLE: return FCMP_UGT;
3355 case FCMP_UEQ: return FCMP_ONE;
3356 case FCMP_UNE: return FCMP_OEQ;
3357 case FCMP_UGT: return FCMP_OLE;
3358 case FCMP_ULT: return FCMP_OGE;
3359 case FCMP_UGE: return FCMP_OLT;
3360 case FCMP_ULE: return FCMP_OGT;
3361 case FCMP_ORD: return FCMP_UNO;
3362 case FCMP_UNO: return FCMP_ORD;
3363 case FCMP_TRUE: return FCMP_FALSE;
3364 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003365 }
3366}
3367
Pete Cooper1d078692015-12-14 20:29:16 +00003368void ICmpInst::anchor() {}
3369
Reid Spencer266e42b2006-12-23 06:05:41 +00003370ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3371 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003372 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003373 case ICMP_EQ: case ICMP_NE:
3374 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3375 return pred;
3376 case ICMP_UGT: return ICMP_SGT;
3377 case ICMP_ULT: return ICMP_SLT;
3378 case ICMP_UGE: return ICMP_SGE;
3379 case ICMP_ULE: return ICMP_SLE;
3380 }
3381}
3382
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003383ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3384 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003385 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003386 case ICMP_EQ: case ICMP_NE:
3387 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3388 return pred;
3389 case ICMP_SGT: return ICMP_UGT;
3390 case ICMP_SLT: return ICMP_ULT;
3391 case ICMP_SGE: return ICMP_UGE;
3392 case ICMP_SLE: return ICMP_ULE;
3393 }
3394}
3395
Reid Spencer0286bc12007-02-28 22:00:54 +00003396/// Initialize a set of values that all satisfy the condition with C.
3397///
3398ConstantRange
3399ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3400 APInt Lower(C);
3401 APInt Upper(C);
3402 uint32_t BitWidth = C.getBitWidth();
3403 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003404 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003405 case ICmpInst::ICMP_EQ: ++Upper; break;
3406 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003407 case ICmpInst::ICMP_ULT:
3408 Lower = APInt::getMinValue(BitWidth);
3409 // Check for an empty-set condition.
3410 if (Lower == Upper)
3411 return ConstantRange(BitWidth, /*isFullSet=*/false);
3412 break;
3413 case ICmpInst::ICMP_SLT:
3414 Lower = APInt::getSignedMinValue(BitWidth);
3415 // Check for an empty-set condition.
3416 if (Lower == Upper)
3417 return ConstantRange(BitWidth, /*isFullSet=*/false);
3418 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003419 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003420 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003421 // Check for an empty-set condition.
3422 if (Lower == Upper)
3423 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003424 break;
3425 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003426 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003427 // Check for an empty-set condition.
3428 if (Lower == Upper)
3429 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003430 break;
3431 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003432 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003433 // Check for a full-set condition.
3434 if (Lower == Upper)
3435 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003436 break;
3437 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003438 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003439 // Check for a full-set condition.
3440 if (Lower == Upper)
3441 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003442 break;
3443 case ICmpInst::ICMP_UGE:
3444 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003445 // Check for a full-set condition.
3446 if (Lower == Upper)
3447 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003448 break;
3449 case ICmpInst::ICMP_SGE:
3450 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003451 // Check for a full-set condition.
3452 if (Lower == Upper)
3453 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003454 break;
3455 }
3456 return ConstantRange(Lower, Upper);
3457}
3458
Dan Gohman4e724382008-05-31 02:47:54 +00003459CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003460 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003461 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003462 case ICMP_EQ: case ICMP_NE:
3463 return pred;
3464 case ICMP_SGT: return ICMP_SLT;
3465 case ICMP_SLT: return ICMP_SGT;
3466 case ICMP_SGE: return ICMP_SLE;
3467 case ICMP_SLE: return ICMP_SGE;
3468 case ICMP_UGT: return ICMP_ULT;
3469 case ICMP_ULT: return ICMP_UGT;
3470 case ICMP_UGE: return ICMP_ULE;
3471 case ICMP_ULE: return ICMP_UGE;
3472
Reid Spencerd9436b62006-11-20 01:22:35 +00003473 case FCMP_FALSE: case FCMP_TRUE:
3474 case FCMP_OEQ: case FCMP_ONE:
3475 case FCMP_UEQ: case FCMP_UNE:
3476 case FCMP_ORD: case FCMP_UNO:
3477 return pred;
3478 case FCMP_OGT: return FCMP_OLT;
3479 case FCMP_OLT: return FCMP_OGT;
3480 case FCMP_OGE: return FCMP_OLE;
3481 case FCMP_OLE: return FCMP_OGE;
3482 case FCMP_UGT: return FCMP_ULT;
3483 case FCMP_ULT: return FCMP_UGT;
3484 case FCMP_UGE: return FCMP_ULE;
3485 case FCMP_ULE: return FCMP_UGE;
3486 }
3487}
3488
Sanjoy Das6e78b172015-10-22 19:57:34 +00003489CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3490 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3491
3492 switch (pred) {
3493 default:
3494 llvm_unreachable("Unknown predicate!");
3495 case CmpInst::ICMP_ULT:
3496 return CmpInst::ICMP_SLT;
3497 case CmpInst::ICMP_ULE:
3498 return CmpInst::ICMP_SLE;
3499 case CmpInst::ICMP_UGT:
3500 return CmpInst::ICMP_SGT;
3501 case CmpInst::ICMP_UGE:
3502 return CmpInst::ICMP_SGE;
3503 }
3504}
3505
Craig Topper1c3f2832015-12-15 06:11:33 +00003506bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003507 switch (predicate) {
3508 default: return false;
3509 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3510 case ICmpInst::ICMP_UGE: return true;
3511 }
3512}
3513
Craig Topper1c3f2832015-12-15 06:11:33 +00003514bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003515 switch (predicate) {
3516 default: return false;
3517 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3518 case ICmpInst::ICMP_SGE: return true;
3519 }
3520}
3521
Craig Topper1c3f2832015-12-15 06:11:33 +00003522bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003523 switch (predicate) {
3524 default: return false;
3525 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3526 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3527 case FCmpInst::FCMP_ORD: return true;
3528 }
3529}
3530
Craig Topper1c3f2832015-12-15 06:11:33 +00003531bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003532 switch (predicate) {
3533 default: return false;
3534 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3535 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3536 case FCmpInst::FCMP_UNO: return true;
3537 }
3538}
3539
Craig Topper1c3f2832015-12-15 06:11:33 +00003540bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003541 switch(predicate) {
3542 default: return false;
3543 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3544 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3545 }
3546}
3547
Craig Topper1c3f2832015-12-15 06:11:33 +00003548bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003549 switch(predicate) {
3550 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3551 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3552 default: return false;
3553 }
3554}
3555
Chad Rosier99bc4802016-04-21 16:18:02 +00003556bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003557 // If the predicates match, then we know the first condition implies the
3558 // second is true.
3559 if (Pred1 == Pred2)
3560 return true;
3561
3562 switch (Pred1) {
3563 default:
3564 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003565 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003566 // A == B implies A >=u B, A <=u B, A >=s B, and A <=s B are true.
Chad Rosier1a601592016-04-22 17:57:34 +00003567 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3568 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003569 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3570 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3571 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3572 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3573 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3574 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3575 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3576 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003577 }
3578 return false;
3579}
3580
Chad Rosier99bc4802016-04-21 16:18:02 +00003581bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003582 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003583}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003584
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003585//===----------------------------------------------------------------------===//
3586// SwitchInst Implementation
3587//===----------------------------------------------------------------------===//
3588
Chris Lattnerbaf00152010-11-17 05:41:46 +00003589void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3590 assert(Value && Default && NumReserved);
3591 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003592 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003593 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003594
Pete Coopereb31b682015-05-21 22:48:54 +00003595 Op<0>() = Value;
3596 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003597}
3598
Chris Lattner2195fc42007-02-24 00:55:48 +00003599/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3600/// switch on and a default destination. The number of additional cases can
3601/// be specified here to make memory allocation more efficient. This
3602/// constructor can also autoinsert before another instruction.
3603SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3604 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003605 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003606 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003607 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003608}
3609
3610/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3611/// switch on and a default destination. The number of additional cases can
3612/// be specified here to make memory allocation more efficient. This
3613/// constructor also autoinserts at the end of the specified BasicBlock.
3614SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3615 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003616 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003617 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003618 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003619}
3620
Misha Brukmanb1c93172005-04-21 23:48:37 +00003621SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003622 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003623 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003624 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003625 Use *OL = getOperandList();
3626 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003627 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003628 OL[i] = InOL[i];
3629 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003630 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003631 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003632}
3633
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003634
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003635/// addCase - Add an entry to the switch instruction...
3636///
Chris Lattner47ac1872005-02-24 05:32:09 +00003637void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003638 unsigned NewCaseIdx = getNumCases();
3639 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003640 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003641 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003642 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003643 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003644 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003645 CaseIt Case(this, NewCaseIdx);
3646 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003647 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003648}
3649
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003650/// removeCase - This method removes the specified case and its successor
3651/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003652void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003653 unsigned idx = i.getCaseIndex();
3654
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003655 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003656
3657 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003658 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003659
Jay Foad14277722011-02-01 09:22:34 +00003660 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003661 if (2 + (idx + 1) * 2 != NumOps) {
3662 OL[2 + idx * 2] = OL[NumOps - 2];
3663 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003664 }
3665
3666 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003667 OL[NumOps-2].set(nullptr);
3668 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003669 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003670}
3671
Jay Foade98f29d2011-04-01 08:00:58 +00003672/// growOperands - grow operands - This grows the operand list in response
3673/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003674///
Jay Foade98f29d2011-04-01 08:00:58 +00003675void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003676 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003677 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003678
3679 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003680 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003681}
3682
3683
3684BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3685 return getSuccessor(idx);
3686}
3687unsigned SwitchInst::getNumSuccessorsV() const {
3688 return getNumSuccessors();
3689}
3690void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3691 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003692}
Chris Lattnerf22be932004-10-15 23:52:53 +00003693
Chris Lattner3ed871f2009-10-27 19:13:16 +00003694//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003695// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003696//===----------------------------------------------------------------------===//
3697
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003698void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003699 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003700 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003701 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003702 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003703 allocHungoffUses(ReservedSpace);
3704
Pete Coopereb31b682015-05-21 22:48:54 +00003705 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003706}
3707
3708
Jay Foade98f29d2011-04-01 08:00:58 +00003709/// growOperands - grow operands - This grows the operand list in response
3710/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003711///
Jay Foade98f29d2011-04-01 08:00:58 +00003712void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003713 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003714 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003715
3716 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003717 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003718}
3719
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003720IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3721 Instruction *InsertBefore)
3722: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003723 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003724 init(Address, NumCases);
3725}
3726
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003727IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3728 BasicBlock *InsertAtEnd)
3729: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003730 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003731 init(Address, NumCases);
3732}
3733
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003734IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003735 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3736 nullptr, IBI.getNumOperands()) {
3737 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003738 Use *OL = getOperandList();
3739 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003740 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3741 OL[i] = InOL[i];
3742 SubclassOptionalData = IBI.SubclassOptionalData;
3743}
3744
Chris Lattner3ed871f2009-10-27 19:13:16 +00003745/// addDestination - Add a destination.
3746///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003747void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003748 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003749 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003750 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003751 // Initialize some new operands.
3752 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003753 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003754 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003755}
3756
3757/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003758/// indirectbr instruction.
3759void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003760 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3761
3762 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003763 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003764
3765 // Replace this value with the last one.
3766 OL[idx+1] = OL[NumOps-1];
3767
3768 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003769 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003770 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003771}
3772
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003773BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003774 return getSuccessor(idx);
3775}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003776unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003777 return getNumSuccessors();
3778}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003779void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003780 setSuccessor(idx, B);
3781}
3782
3783//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003784// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003785//===----------------------------------------------------------------------===//
3786
Chris Lattnerf22be932004-10-15 23:52:53 +00003787// Define these methods here so vtables don't get emitted into every translation
3788// unit that uses these classes.
3789
Pete Cooper75403d72015-06-24 20:22:23 +00003790GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003791 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003792}
3793
Pete Cooper75403d72015-06-24 20:22:23 +00003794BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003795 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003796}
3797
Pete Cooper75403d72015-06-24 20:22:23 +00003798FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003799 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003800}
3801
Pete Cooper75403d72015-06-24 20:22:23 +00003802ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003803 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003804}
3805
Pete Cooper75403d72015-06-24 20:22:23 +00003806ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003807 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003808}
3809
Pete Cooper75403d72015-06-24 20:22:23 +00003810InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003811 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003812}
3813
Pete Cooper75403d72015-06-24 20:22:23 +00003814AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003815 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3816 (Value *)getOperand(0), getAlignment());
3817 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003818 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003819 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003820}
3821
Pete Cooper75403d72015-06-24 20:22:23 +00003822LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003823 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3824 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003825}
3826
Pete Cooper75403d72015-06-24 20:22:23 +00003827StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003828 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003829 getAlignment(), getOrdering(), getSynchScope());
3830
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003831}
3832
Pete Cooper75403d72015-06-24 20:22:23 +00003833AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003834 AtomicCmpXchgInst *Result =
3835 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003836 getSuccessOrdering(), getFailureOrdering(),
3837 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003838 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003839 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003840 return Result;
3841}
3842
Pete Cooper75403d72015-06-24 20:22:23 +00003843AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003844 AtomicRMWInst *Result =
3845 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3846 getOrdering(), getSynchScope());
3847 Result->setVolatile(isVolatile());
3848 return Result;
3849}
3850
Pete Cooper75403d72015-06-24 20:22:23 +00003851FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003852 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3853}
3854
Pete Cooper75403d72015-06-24 20:22:23 +00003855TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003856 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003857}
3858
Pete Cooper75403d72015-06-24 20:22:23 +00003859ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003860 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003861}
3862
Pete Cooper75403d72015-06-24 20:22:23 +00003863SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003864 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003865}
3866
Pete Cooper75403d72015-06-24 20:22:23 +00003867FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003868 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003869}
3870
Pete Cooper75403d72015-06-24 20:22:23 +00003871FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003872 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003873}
3874
Pete Cooper75403d72015-06-24 20:22:23 +00003875UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003876 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003880 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003881}
3882
Pete Cooper75403d72015-06-24 20:22:23 +00003883FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003892 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003893}
3894
Pete Cooper75403d72015-06-24 20:22:23 +00003895IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003896 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003897}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003898
Pete Cooper75403d72015-06-24 20:22:23 +00003899BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003900 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003901}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003902
Pete Cooper75403d72015-06-24 20:22:23 +00003903AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003904 return new AddrSpaceCastInst(getOperand(0), getType());
3905}
3906
Pete Cooper75403d72015-06-24 20:22:23 +00003907CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003908 if (hasOperandBundles()) {
3909 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3910 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3911 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003912 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003913}
3914
Pete Cooper75403d72015-06-24 20:22:23 +00003915SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003916 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003917}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003918
Pete Cooper75403d72015-06-24 20:22:23 +00003919VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003920 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003921}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003922
Pete Cooper75403d72015-06-24 20:22:23 +00003923ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003924 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003925}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003926
Pete Cooper75403d72015-06-24 20:22:23 +00003927InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003928 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003929}
3930
Pete Cooper75403d72015-06-24 20:22:23 +00003931ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003932 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003933}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003934
Pete Cooper75403d72015-06-24 20:22:23 +00003935PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003936
Pete Cooper75403d72015-06-24 20:22:23 +00003937LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003938 return new LandingPadInst(*this);
3939}
3940
Pete Cooper75403d72015-06-24 20:22:23 +00003941ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003942 return new(getNumOperands()) ReturnInst(*this);
3943}
3944
Pete Cooper75403d72015-06-24 20:22:23 +00003945BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003946 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003947}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003948
Pete Cooper75403d72015-06-24 20:22:23 +00003949SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003950
Pete Cooper75403d72015-06-24 20:22:23 +00003951IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003952 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003953}
3954
Pete Cooper75403d72015-06-24 20:22:23 +00003955InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003956 if (hasOperandBundles()) {
3957 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3958 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3959 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003960 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003961}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003962
Pete Cooper75403d72015-06-24 20:22:23 +00003963ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003964
David Majnemer654e1302015-07-31 17:58:14 +00003965CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3966 return new (getNumOperands()) CleanupReturnInst(*this);
3967}
3968
David Majnemer654e1302015-07-31 17:58:14 +00003969CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003970 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003971}
3972
David Majnemer8a1c45d2015-12-12 05:38:55 +00003973CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3974 return new CatchSwitchInst(*this);
3975}
3976
3977FuncletPadInst *FuncletPadInst::cloneImpl() const {
3978 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003979}
3980
Pete Cooper75403d72015-06-24 20:22:23 +00003981UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003982 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003983 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003984}