blob: 21a0042d26e3c6792ec2d5972f7f524a7d2718ac [file] [log] [blame]
Eugene Zelenkod761e2c2017-05-15 21:57:41 +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 Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000017#include "llvm/ADT/None.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/IR/Attributes.h"
21#include "llvm/IR/BasicBlock.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000022#include "llvm/IR/CallSite.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000023#include "llvm/IR/Constant.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Function.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000028#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Module.h"
33#include "llvm/IR/Operator.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000034#include "llvm/IR/Type.h"
35#include "llvm/IR/Value.h"
36#include "llvm/Support/AtomicOrdering.h"
37#include "llvm/Support/Casting.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000039#include "llvm/Support/MathExtras.h"
Eugene Zelenkod761e2c2017-05-15 21:57:41 +000040#include <algorithm>
41#include <cassert>
42#include <cstdint>
43#include <vector>
44
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000045using namespace llvm;
46
Chris Lattner3e13b8c2008-01-02 23:42:30 +000047//===----------------------------------------------------------------------===//
48// CallSite Class
49//===----------------------------------------------------------------------===//
50
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000051User::op_iterator CallSite::getCallee() const {
52 Instruction *II(getInstruction());
53 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000054 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000055 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000056}
57
Gordon Henriksen14a55692007-12-10 02:14:30 +000058//===----------------------------------------------------------------------===//
59// TerminatorInst Class
60//===----------------------------------------------------------------------===//
61
Reid Kleckner45a13e12017-05-11 21:26:55 +000062unsigned TerminatorInst::getNumSuccessors() const {
63 switch (getOpcode()) {
64#define HANDLE_TERM_INST(N, OPC, CLASS) \
65 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000066 return static_cast<const CLASS *>(this)->getNumSuccessors();
Reid Kleckner45a13e12017-05-11 21:26:55 +000067#include "llvm/IR/Instruction.def"
68 default:
69 break;
70 }
71 llvm_unreachable("not a terminator");
72}
73
74BasicBlock *TerminatorInst::getSuccessor(unsigned idx) const {
75 switch (getOpcode()) {
76#define HANDLE_TERM_INST(N, OPC, CLASS) \
77 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000078 return static_cast<const CLASS *>(this)->getSuccessor(idx);
Reid Kleckner45a13e12017-05-11 21:26:55 +000079#include "llvm/IR/Instruction.def"
80 default:
81 break;
82 }
83 llvm_unreachable("not a terminator");
84}
85
86void TerminatorInst::setSuccessor(unsigned idx, BasicBlock *B) {
87 switch (getOpcode()) {
88#define HANDLE_TERM_INST(N, OPC, CLASS) \
89 case Instruction::OPC: \
Craig Topperc1993fa2017-06-08 23:23:08 +000090 return static_cast<CLASS *>(this)->setSuccessor(idx, B);
Reid Kleckner45a13e12017-05-11 21:26:55 +000091#include "llvm/IR/Instruction.def"
92 default:
93 break;
94 }
95 llvm_unreachable("not a terminator");
96}
97
Gabor Greiff6caff662008-05-10 08:32:32 +000098//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000099// SelectInst Class
100//===----------------------------------------------------------------------===//
101
102/// areInvalidOperands - Return a string if the specified operands are invalid
103/// for a select operation, otherwise return null.
104const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
105 if (Op1->getType() != Op2->getType())
106 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +0000107
108 if (Op1->getType()->isTokenTy())
109 return "select values cannot have token type";
110
Chris Lattner229907c2011-07-18 04:54:35 +0000111 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +0000112 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +0000113 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +0000114 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +0000115 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +0000116 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +0000117 return "selected values for vector select must be vectors";
118 if (ET->getNumElements() != VT->getNumElements())
119 return "vector select requires selected vectors to have "
120 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +0000121 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +0000122 return "select condition must be i1 or <n x i1>";
123 }
Craig Topperc6207612014-04-09 06:08:46 +0000124 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +0000125}
126
Chris Lattner88107952008-12-29 00:12:50 +0000127//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000128// PHINode Class
129//===----------------------------------------------------------------------===//
130
131PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +0000132 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
133 ReservedSpace(PN.getNumOperands()) {
134 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +0000135 std::copy(PN.op_begin(), PN.op_end(), op_begin());
136 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000137 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000140// removeIncomingValue - Remove an incoming value. This is useful if a
141// predecessor basic block is deleted.
142Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000143 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000144
145 // Move everything after this operand down.
146 //
147 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000148 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000149 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000150 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
151 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000152
153 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000154 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000155 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000156
157 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000158 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000159 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000160 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161 eraseFromParent();
162 }
163 return Removed;
164}
165
Jay Foade98f29d2011-04-01 08:00:58 +0000166/// growOperands - grow operands - This grows the operand list in response
167/// to a push_back style of operation. This grows the number of ops by 1.5
168/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000169///
Jay Foade98f29d2011-04-01 08:00:58 +0000170void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000171 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000172 unsigned NumOps = e + e / 2;
173 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
174
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000175 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000176 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000177}
178
Nate Begemanb3923212005-08-04 23:24:19 +0000179/// hasConstantValue - If the specified PHI node always merges together the same
180/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000181Value *PHINode::hasConstantValue() const {
182 // Exploit the fact that phi nodes always have at least one entry.
183 Value *ConstantValue = getIncomingValue(0);
184 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000185 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
186 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000187 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000188 // The case where the first value is this PHI.
189 ConstantValue = getIncomingValue(i);
190 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000191 if (ConstantValue == this)
192 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000193 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000194}
195
Nicolai Haehnle13d90f32016-04-14 17:42:47 +0000196/// hasConstantOrUndefValue - Whether the specified PHI node always merges
197/// together the same value, assuming that undefs result in the same value as
198/// non-undefs.
199/// Unlike \ref hasConstantValue, this does not return a value because the
200/// unique non-undef incoming value need not dominate the PHI node.
201bool PHINode::hasConstantOrUndefValue() const {
202 Value *ConstantValue = nullptr;
203 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) {
204 Value *Incoming = getIncomingValue(i);
205 if (Incoming != this && !isa<UndefValue>(Incoming)) {
206 if (ConstantValue && ConstantValue != Incoming)
207 return false;
208 ConstantValue = Incoming;
209 }
210 }
211 return true;
212}
213
Bill Wendlingfae14752011-08-12 20:24:12 +0000214//===----------------------------------------------------------------------===//
215// LandingPadInst Implementation
216//===----------------------------------------------------------------------===//
217
David Majnemer7fddecc2015-06-17 20:52:32 +0000218LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
219 const Twine &NameStr, Instruction *InsertBefore)
220 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
221 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000222}
223
David Majnemer7fddecc2015-06-17 20:52:32 +0000224LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
225 const Twine &NameStr, BasicBlock *InsertAtEnd)
226 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
227 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000228}
229
230LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000231 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
232 LP.getNumOperands()),
233 ReservedSpace(LP.getNumOperands()) {
234 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000235 Use *OL = getOperandList();
236 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000237 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
238 OL[I] = InOL[I];
239
240 setCleanup(LP.isCleanup());
241}
242
David Majnemer7fddecc2015-06-17 20:52:32 +0000243LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000244 const Twine &NameStr,
245 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000246 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000247}
248
David Majnemer7fddecc2015-06-17 20:52:32 +0000249LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000250 const Twine &NameStr,
251 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000252 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000253}
254
David Majnemer7fddecc2015-06-17 20:52:32 +0000255void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000256 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000257 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000258 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000259 setName(NameStr);
260 setCleanup(false);
261}
262
263/// growOperands - grow operands - This grows the operand list in response to a
264/// push_back style of operation. This grows the number of ops by 2 times.
265void LandingPadInst::growOperands(unsigned Size) {
266 unsigned e = getNumOperands();
267 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000268 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000269 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000270}
271
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000272void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000273 unsigned OpNo = getNumOperands();
274 growOperands(1);
275 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000276 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000277 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000278}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000279
280//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281// CallInst Implementation
282//===----------------------------------------------------------------------===//
283
David Blaikie348de692015-04-23 21:36:23 +0000284void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000285 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000286 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000287 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
288 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000289 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290
Jay Foad5bd375a2011-07-15 08:37:34 +0000291#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000292 assert((Args.size() == FTy->getNumParams() ||
293 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000294 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000295
296 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000297 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000298 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000299 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000300#endif
301
302 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000303
304 auto It = populateBundleOperandInfos(Bundles, Args.size());
305 (void)It;
306 assert(It + 1 == op_end() && "Should add up!");
307
Jay Foad5bd375a2011-07-15 08:37:34 +0000308 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Jay Foad5bd375a2011-07-15 08:37:34 +0000311void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000312 FTy =
313 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000314 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000315 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000316
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000317 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000318
319 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000322CallInst::CallInst(Value *Func, const Twine &Name, Instruction *InsertBefore)
323 : CallBase<CallInst>(
324 cast<FunctionType>(
325 cast<PointerType>(Func->getType())->getElementType())
326 ->getReturnType(),
327 Instruction::Call,
328 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1,
329 InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000330 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000333CallInst::CallInst(Value *Func, const Twine &Name, BasicBlock *InsertAtEnd)
334 : CallBase<CallInst>(
335 cast<FunctionType>(
336 cast<PointerType>(Func->getType())->getElementType())
337 ->getReturnType(),
338 Instruction::Call,
339 OperandTraits<CallBase<CallInst>>::op_end(this) - 1, 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000340 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
342
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343CallInst::CallInst(const CallInst &CI)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000344 : CallBase<CallInst>(CI.Attrs, CI.FTy, CI.getType(), Instruction::Call,
345 OperandTraits<CallBase<CallInst>>::op_end(this) -
346 CI.getNumOperands(),
347 CI.getNumOperands()) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000348 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000349 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000350
Jay Foad5bd375a2011-07-15 08:37:34 +0000351 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000352 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
353 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000354 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000355}
356
Sanjoy Das2d161452015-11-18 06:23:38 +0000357CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
358 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000359 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000360
361 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
362 InsertPt);
363 NewCI->setTailCallKind(CI->getTailCallKind());
364 NewCI->setCallingConv(CI->getCallingConv());
365 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000366 NewCI->setAttributes(CI->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000367 NewCI->setDebugLoc(CI->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000368 return NewCI;
369}
370
Hal Finkele87ad542016-07-10 23:01:32 +0000371
Reid Klecknera0b45f42017-05-03 18:17:31 +0000372
Hal Finkele87ad542016-07-10 23:01:32 +0000373
Eric Christopher901b1a72008-05-16 20:39:43 +0000374
Amaury Secheta65a2372016-06-15 05:14:29 +0000375
Reid Kleckner5fbdd172017-05-31 19:23:09 +0000376
Reid Klecknera0b45f42017-05-03 18:17:31 +0000377
Amaury Sechet1a0e0972016-04-21 21:29:10 +0000378
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000379
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380/// IsConstantOne - Return true only if val is constant int 1
381static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000382 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000383 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
384 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000385}
386
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000387static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000388 BasicBlock *InsertAtEnd, Type *IntPtrTy,
David Majnemerfadc6db2016-04-29 08:07:22 +0000389 Type *AllocTy, Value *AllocSize,
390 Value *ArraySize,
391 ArrayRef<OperandBundleDef> OpB,
392 Function *MallocF, const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000393 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000394 "createMalloc needs either InsertBefore or InsertAtEnd");
395
396 // malloc(type) becomes:
397 // bitcast (i8* malloc(typeSize)) to type*
398 // malloc(type, arraySize) becomes:
Ana Pazosb3596022016-02-03 21:34:39 +0000399 // bitcast (i8* malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000400 if (!ArraySize)
401 ArraySize = ConstantInt::get(IntPtrTy, 1);
402 else if (ArraySize->getType() != IntPtrTy) {
403 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000404 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
405 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000406 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000407 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
408 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000409 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000411 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000412 if (IsConstantOne(AllocSize)) {
413 AllocSize = ArraySize; // Operand * 1 = Operand
414 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
415 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
416 false /*ZExt*/);
417 // Malloc arg is constant product of type size and array size
418 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
419 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000420 // Multiply type size by the array size...
421 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000422 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
423 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000424 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000425 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
426 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000428 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000429
Victor Hernandez788eaab2009-09-18 19:20:02 +0000430 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000431 // Create the call to Malloc.
Ana Pazosb3596022016-02-03 21:34:39 +0000432 BasicBlock *BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
433 Module *M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000434 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000435 Value *MallocFunc = MallocF;
436 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000437 // prototype malloc as "void *malloc(size_t)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000438 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
Chris Lattner229907c2011-07-18 04:54:35 +0000439 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000440 CallInst *MCall = nullptr;
441 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000442 if (InsertBefore) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000443 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall",
444 InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000445 Result = MCall;
446 if (Result->getType() != AllocPtrType)
447 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000448 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000449 } else {
David Majnemerfadc6db2016-04-29 08:07:22 +0000450 MCall = CallInst::Create(MallocFunc, AllocSize, OpB, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000451 Result = MCall;
452 if (Result->getType() != AllocPtrType) {
453 InsertAtEnd->getInstList().push_back(MCall);
454 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000455 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000456 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000457 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000458 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000459 if (Function *F = dyn_cast<Function>(MallocFunc)) {
460 MCall->setCallingConv(F->getCallingConv());
Reid Klecknera0b45f42017-05-03 18:17:31 +0000461 if (!F->returnDoesNotAlias())
462 F->setReturnDoesNotAlias();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000463 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000464 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000465
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000466 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000467}
468
469/// CreateMalloc - Generate the IR for a call to malloc:
470/// 1. Compute the malloc call's argument as the specified type's size,
471/// possibly multiplied by the array size if the array size is not
472/// constant 1.
473/// 2. Call malloc with that argument.
474/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000475Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000476 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000477 Value *AllocSize, Value *ArraySize,
Ana Pazosb3596022016-02-03 21:34:39 +0000478 Function *MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000479 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000480 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000481 ArraySize, None, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000482}
David Majnemerfadc6db2016-04-29 08:07:22 +0000483Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
484 Type *IntPtrTy, Type *AllocTy,
485 Value *AllocSize, Value *ArraySize,
486 ArrayRef<OperandBundleDef> OpB,
487 Function *MallocF,
488 const Twine &Name) {
489 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
490 ArraySize, OpB, MallocF, Name);
491}
492
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000493/// CreateMalloc - Generate the IR for a call to malloc:
494/// 1. Compute the malloc call's argument as the specified type's size,
495/// possibly multiplied by the array size if the array size is not
496/// constant 1.
497/// 2. Call malloc with that argument.
498/// 3. Bitcast the result of the malloc call to the specified type.
499/// Note: This function does not add the bitcast to the basic block, that is the
500/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000501Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000502 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000503 Value *AllocSize, Value *ArraySize,
504 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000505 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
David Majnemerfadc6db2016-04-29 08:07:22 +0000506 ArraySize, None, MallocF, Name);
507}
508Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
509 Type *IntPtrTy, Type *AllocTy,
510 Value *AllocSize, Value *ArraySize,
511 ArrayRef<OperandBundleDef> OpB,
512 Function *MallocF, const Twine &Name) {
513 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
514 ArraySize, OpB, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000515}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000516
David Majnemerfadc6db2016-04-29 08:07:22 +0000517static Instruction *createFree(Value *Source,
518 ArrayRef<OperandBundleDef> Bundles,
519 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*)"
Serge Guelton59a2d7b2017-04-11 15:01:18 +0000532 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
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);
David Majnemerfadc6db2016-04-29 08:07:22 +0000538 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "", InsertBefore);
Victor Hernandeze2971492009-10-24 04:23:03 +0000539 } else {
540 if (Source->getType() != IntPtrTy)
541 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
David Majnemerfadc6db2016-04-29 08:07:22 +0000542 Result = CallInst::Create(FreeFunc, PtrCast, Bundles, "");
Victor Hernandeze2971492009-10-24 04:23:03 +0000543 }
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) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000553 return createFree(Source, None, InsertBefore, nullptr);
554}
555Instruction *CallInst::CreateFree(Value *Source,
556 ArrayRef<OperandBundleDef> Bundles,
557 Instruction *InsertBefore) {
558 return createFree(Source, Bundles, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000559}
560
561/// CreateFree - Generate the IR for a call to the builtin free function.
562/// Note: This function does not add the call to the basic block, that is the
563/// responsibility of the caller.
Ana Pazosb3596022016-02-03 21:34:39 +0000564Instruction *CallInst::CreateFree(Value *Source, BasicBlock *InsertAtEnd) {
David Majnemerfadc6db2016-04-29 08:07:22 +0000565 Instruction *FreeCall = createFree(Source, None, nullptr, InsertAtEnd);
566 assert(FreeCall && "CreateFree did not create a CallInst");
567 return FreeCall;
568}
569Instruction *CallInst::CreateFree(Value *Source,
570 ArrayRef<OperandBundleDef> Bundles,
571 BasicBlock *InsertAtEnd) {
572 Instruction *FreeCall = createFree(Source, Bundles, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000573 assert(FreeCall && "CreateFree did not create a CallInst");
574 return FreeCall;
575}
576
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000577//===----------------------------------------------------------------------===//
578// InvokeInst Implementation
579//===----------------------------------------------------------------------===//
580
David Blaikie3e807092015-05-13 18:35:26 +0000581void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
582 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000583 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000584 const Twine &NameStr) {
585 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000586
Sanjoy Das9303c242015-09-24 19:14:18 +0000587 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
588 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000589 Op<-3>() = Fn;
590 Op<-2>() = IfNormal;
591 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000592
593#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000594 assert(((Args.size() == FTy->getNumParams()) ||
595 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000596 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000597
Jay Foad5bd375a2011-07-15 08:37:34 +0000598 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000599 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000600 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000601 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000602#endif
603
604 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000605
606 auto It = populateBundleOperandInfos(Bundles, Args.size());
607 (void)It;
608 assert(It + 3 == op_end() && "Should add up!");
609
Jay Foad5bd375a2011-07-15 08:37:34 +0000610 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000611}
612
Misha Brukmanb1c93172005-04-21 23:48:37 +0000613InvokeInst::InvokeInst(const InvokeInst &II)
Serge Guelton1fb81bc2018-02-22 13:30:32 +0000614 : CallBase<InvokeInst>(II.Attrs, II.FTy, II.getType(), Instruction::Invoke,
615 OperandTraits<CallBase<InvokeInst>>::op_end(this) -
616 II.getNumOperands(),
617 II.getNumOperands()) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000618 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000619 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000620 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
621 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000622 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623}
624
Sanjoy Das2d161452015-11-18 06:23:38 +0000625InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
626 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000627 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000628
629 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
630 II->getUnwindDest(), Args, OpB,
631 II->getName(), InsertPt);
632 NewII->setCallingConv(II->getCallingConv());
633 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000634 NewII->setAttributes(II->getAttributes());
Joseph Tremouletbba70e42016-01-14 06:21:42 +0000635 NewII->setDebugLoc(II->getDebugLoc());
Sanjoy Das2d161452015-11-18 06:23:38 +0000636 return NewII;
637}
638
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000639
Bill Wendlingfae14752011-08-12 20:24:12 +0000640LandingPadInst *InvokeInst::getLandingPadInst() const {
641 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
642}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000643
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000644//===----------------------------------------------------------------------===//
645// ReturnInst Implementation
646//===----------------------------------------------------------------------===//
647
Chris Lattner2195fc42007-02-24 00:55:48 +0000648ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000649 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000650 OperandTraits<ReturnInst>::op_end(this) -
651 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000652 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000653 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000654 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000655 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000656}
657
Owen Anderson55f1c092009-08-13 21:58:54 +0000658ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
659 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000660 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
661 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000662 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000663 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000664}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000665
Owen Anderson55f1c092009-08-13 21:58:54 +0000666ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
667 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000668 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
669 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000670 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000671 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000672}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000673
Owen Anderson55f1c092009-08-13 21:58:54 +0000674ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
675 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000676 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000677}
678
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000679//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000680// ResumeInst Implementation
681//===----------------------------------------------------------------------===//
682
683ResumeInst::ResumeInst(const ResumeInst &RI)
684 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
685 OperandTraits<ResumeInst>::op_begin(this), 1) {
686 Op<0>() = RI.Op<0>();
687}
688
689ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
690 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
691 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
692 Op<0>() = Exn;
693}
694
695ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
696 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
697 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
698 Op<0>() = Exn;
699}
700
Bill Wendlingf891bf82011-07-31 06:30:59 +0000701//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000702// CleanupReturnInst Implementation
703//===----------------------------------------------------------------------===//
704
705CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
706 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
707 OperandTraits<CleanupReturnInst>::op_end(this) -
708 CRI.getNumOperands(),
709 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000710 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000711 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000712 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000713 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000714}
715
David Majnemer8a1c45d2015-12-12 05:38:55 +0000716void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000717 if (UnwindBB)
718 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000719
David Majnemer8a1c45d2015-12-12 05:38:55 +0000720 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000721 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000722 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000723}
724
David Majnemer8a1c45d2015-12-12 05:38:55 +0000725CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
726 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000727 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
728 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000729 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
730 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000731 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000732}
733
David Majnemer8a1c45d2015-12-12 05:38:55 +0000734CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
735 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000736 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
737 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000738 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
739 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000740 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000741}
742
David Majnemer654e1302015-07-31 17:58:14 +0000743//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000744// CatchReturnInst Implementation
745//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000746void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000747 Op<0>() = CatchPad;
748 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000749}
David Majnemer654e1302015-07-31 17:58:14 +0000750
751CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
752 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000753 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
754 Op<0>() = CRI.Op<0>();
755 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000756}
757
David Majnemer8a1c45d2015-12-12 05:38:55 +0000758CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000759 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000760 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000761 OperandTraits<CatchReturnInst>::op_begin(this), 2,
762 InsertBefore) {
763 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000764}
765
David Majnemer8a1c45d2015-12-12 05:38:55 +0000766CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000767 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000768 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000769 OperandTraits<CatchReturnInst>::op_begin(this), 2,
770 InsertAtEnd) {
771 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000772}
773
David Majnemer654e1302015-07-31 17:58:14 +0000774//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000775// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000776//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000777
778CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
779 unsigned NumReservedValues,
780 const Twine &NameStr,
781 Instruction *InsertBefore)
782 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
783 InsertBefore) {
784 if (UnwindDest)
785 ++NumReservedValues;
786 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000787 setName(NameStr);
788}
789
David Majnemer8a1c45d2015-12-12 05:38:55 +0000790CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
791 unsigned NumReservedValues,
792 const Twine &NameStr, BasicBlock *InsertAtEnd)
793 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000794 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000795 if (UnwindDest)
796 ++NumReservedValues;
797 init(ParentPad, UnwindDest, NumReservedValues + 1);
798 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000799}
800
David Majnemer8a1c45d2015-12-12 05:38:55 +0000801CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
802 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
803 CSI.getNumOperands()) {
804 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
805 setNumHungOffUseOperands(ReservedSpace);
806 Use *OL = getOperandList();
807 const Use *InOL = CSI.getOperandList();
808 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
809 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000810}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000811
812void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
813 unsigned NumReservedValues) {
814 assert(ParentPad && NumReservedValues);
815
816 ReservedSpace = NumReservedValues;
817 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
818 allocHungoffUses(ReservedSpace);
819
820 Op<0>() = ParentPad;
821 if (UnwindDest) {
822 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
823 setUnwindDest(UnwindDest);
824 }
825}
826
827/// growOperands - grow operands - This grows the operand list in response to a
828/// push_back style of operation. This grows the number of ops by 2 times.
829void CatchSwitchInst::growOperands(unsigned Size) {
830 unsigned NumOperands = getNumOperands();
831 assert(NumOperands >= 1);
832 if (ReservedSpace >= NumOperands + Size)
833 return;
834 ReservedSpace = (NumOperands + Size / 2) * 2;
835 growHungoffUses(ReservedSpace);
836}
837
838void CatchSwitchInst::addHandler(BasicBlock *Handler) {
839 unsigned OpNo = getNumOperands();
840 growOperands(1);
841 assert(OpNo < ReservedSpace && "Growing didn't work!");
842 setNumHungOffUseOperands(getNumOperands() + 1);
843 getOperandList()[OpNo] = Handler;
844}
845
Joseph Tremoulet0d808882016-01-05 02:37:41 +0000846void CatchSwitchInst::removeHandler(handler_iterator HI) {
847 // Move all subsequent handlers up one.
848 Use *EndDst = op_end() - 1;
849 for (Use *CurDst = HI.getCurrent(); CurDst != EndDst; ++CurDst)
850 *CurDst = *(CurDst + 1);
851 // Null out the last handler use.
852 *EndDst = nullptr;
853
854 setNumHungOffUseOperands(getNumOperands() - 1);
855}
856
David Majnemer8a1c45d2015-12-12 05:38:55 +0000857//===----------------------------------------------------------------------===//
858// FuncletPadInst Implementation
859//===----------------------------------------------------------------------===//
860void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
861 const Twine &NameStr) {
862 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
863 std::copy(Args.begin(), Args.end(), op_begin());
864 setParentPad(ParentPad);
865 setName(NameStr);
866}
867
868FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
869 : Instruction(FPI.getType(), FPI.getOpcode(),
870 OperandTraits<FuncletPadInst>::op_end(this) -
871 FPI.getNumOperands(),
872 FPI.getNumOperands()) {
873 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
874 setParentPad(FPI.getParentPad());
875}
876
877FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
878 ArrayRef<Value *> Args, unsigned Values,
879 const Twine &NameStr, Instruction *InsertBefore)
880 : Instruction(ParentPad->getType(), Op,
881 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
882 InsertBefore) {
883 init(ParentPad, Args, NameStr);
884}
885
886FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
887 ArrayRef<Value *> Args, unsigned Values,
888 const Twine &NameStr, BasicBlock *InsertAtEnd)
889 : Instruction(ParentPad->getType(), Op,
890 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
891 InsertAtEnd) {
892 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000893}
894
895//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000896// UnreachableInst Implementation
897//===----------------------------------------------------------------------===//
898
Owen Anderson55f1c092009-08-13 21:58:54 +0000899UnreachableInst::UnreachableInst(LLVMContext &Context,
900 Instruction *InsertBefore)
901 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000902 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000903}
Owen Anderson55f1c092009-08-13 21:58:54 +0000904UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
905 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000906 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000907}
908
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000909//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000910// BranchInst Implementation
911//===----------------------------------------------------------------------===//
912
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000913void BranchInst::AssertOK() {
914 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000915 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000916 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000917}
918
Chris Lattner2195fc42007-02-24 00:55:48 +0000919BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000920 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000921 OperandTraits<BranchInst>::op_end(this) - 1,
922 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000923 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000924 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000925}
Eugene Zelenkod761e2c2017-05-15 21:57:41 +0000926
Chris Lattner2195fc42007-02-24 00:55:48 +0000927BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
928 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000929 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000930 OperandTraits<BranchInst>::op_end(this) - 3,
931 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000932 Op<-1>() = IfTrue;
933 Op<-2>() = IfFalse;
934 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000935#ifndef NDEBUG
936 AssertOK();
937#endif
938}
939
940BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000941 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000942 OperandTraits<BranchInst>::op_end(this) - 1,
943 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000944 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000945 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000946}
947
948BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
949 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000950 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000951 OperandTraits<BranchInst>::op_end(this) - 3,
952 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000953 Op<-1>() = IfTrue;
954 Op<-2>() = IfFalse;
955 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000956#ifndef NDEBUG
957 AssertOK();
958#endif
959}
960
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000961BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000962 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000963 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
964 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000965 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000966 if (BI.getNumOperands() != 1) {
967 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000968 Op<-3>() = BI.Op<-3>();
969 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000970 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000971 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000972}
973
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000974void BranchInst::swapSuccessors() {
975 assert(isConditional() &&
976 "Cannot swap successors of an unconditional branch");
977 Op<-1>().swap(Op<-2>());
978
979 // Update profile metadata if present and it matches our structural
980 // expectations.
Xinliang David Lidc491402016-08-23 15:39:03 +0000981 swapProfMetadata();
Chandler Carruth3e8aa652011-10-17 01:11:57 +0000982}
983
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000985// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000986//===----------------------------------------------------------------------===//
987
Owen Andersonb6b25302009-07-14 23:09:55 +0000988static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000989 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000990 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000991 else {
992 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000993 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000994 assert(Amt->getType()->isIntegerTy() &&
995 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000996 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000997 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000998}
999
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001000AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001001 Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001002 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001003
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001004AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001005 BasicBlock *InsertAtEnd)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001006 : AllocaInst(Ty, AddrSpace, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001007
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001008AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001009 const Twine &Name, Instruction *InsertBefore)
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001010 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertBefore) {}
1011
1012AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1013 const Twine &Name, BasicBlock *InsertAtEnd)
1014 : AllocaInst(Ty, AddrSpace, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
1015
1016AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1017 unsigned Align, const Twine &Name,
1018 Instruction *InsertBefore)
1019 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1020 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1021 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001022 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001023 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001024 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001025}
1026
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001027AllocaInst::AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
1028 unsigned Align, const Twine &Name,
1029 BasicBlock *InsertAtEnd)
1030 : UnaryInstruction(PointerType::get(Ty, AddrSpace), Alloca,
1031 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
David Blaikiebf0a42a2015-04-29 23:00:35 +00001032 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001033 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001034 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001035 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001036}
1037
Victor Hernandez8acf2952009-10-23 21:09:37 +00001038void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001039 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001040 assert(Align <= MaximumAlignment &&
1041 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001042 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1043 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001044 assert(getAlignment() == Align && "Alignment representation error!");
1045}
1046
Victor Hernandez8acf2952009-10-23 21:09:37 +00001047bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001048 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001049 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001050 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001051}
1052
Chris Lattner8b291e62008-11-26 02:54:17 +00001053/// isStaticAlloca - Return true if this alloca is in the entry block of the
1054/// function and is a constant size. If so, the code generator will fold it
1055/// into the prolog/epilog code, so it is basically free.
1056bool AllocaInst::isStaticAlloca() const {
1057 // Must be constant size.
1058 if (!isa<ConstantInt>(getArraySize())) return false;
1059
1060 // Must be in the entry block.
1061 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001062 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001063}
1064
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001065//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001066// LoadInst Implementation
1067//===----------------------------------------------------------------------===//
1068
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001069void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001070 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001071 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001072 assert(!(isAtomic() && getAlignment() == 0) &&
1073 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001074}
1075
Daniel Dunbar4975db62009-07-25 04:41:11 +00001076LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001077 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001078
Daniel Dunbar4975db62009-07-25 04:41:11 +00001079LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001080 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001081
David Blaikie0c28fd72015-05-20 21:46:30 +00001082LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001083 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001084 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001085
1086LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1087 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001088 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001089
David Blaikieb7a029872015-04-17 19:56:21 +00001090LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001091 unsigned Align, Instruction *InsertBef)
JF Bastien800f87a2016-04-06 21:19:33 +00001092 : LoadInst(Ty, Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001093 SyncScope::System, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001094
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001095LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001096 unsigned Align, BasicBlock *InsertAE)
JF Bastien800f87a2016-04-06 21:19:33 +00001097 : LoadInst(Ptr, Name, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001098 SyncScope::System, InsertAE) {}
Dan Gohman68659282007-07-18 20:51:11 +00001099
David Blaikie15d9a4c2015-04-06 20:59:48 +00001100LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001101 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001102 SyncScope::ID SSID, Instruction *InsertBef)
David Blaikie15d9a4c2015-04-06 20:59:48 +00001103 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001104 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001105 setVolatile(isVolatile);
1106 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001107 setAtomic(Order, SSID);
Eli Friedman59b66882011-08-09 23:02:53 +00001108 AssertOK();
1109 setName(Name);
1110}
1111
1112LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1113 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001114 SyncScope::ID SSID,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001115 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001116 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001117 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001118 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001119 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001120 setAtomic(Order, SSID);
Chris Lattner0f048162007-02-13 07:54:42 +00001121 AssertOK();
1122 setName(Name);
1123}
1124
Daniel Dunbar27096822009-08-11 18:11:15 +00001125LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1126 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1127 Load, Ptr, InsertBef) {
1128 setVolatile(false);
1129 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001130 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001131 AssertOK();
1132 if (Name && Name[0]) setName(Name);
1133}
1134
1135LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1136 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1137 Load, Ptr, InsertAE) {
1138 setVolatile(false);
1139 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001140 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001141 AssertOK();
1142 if (Name && Name[0]) setName(Name);
1143}
1144
David Blaikie0c28fd72015-05-20 21:46:30 +00001145LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001146 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001147 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1148 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001149 setVolatile(isVolatile);
1150 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001151 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001152 AssertOK();
1153 if (Name && Name[0]) setName(Name);
1154}
1155
1156LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1157 BasicBlock *InsertAE)
1158 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1159 Load, Ptr, InsertAE) {
1160 setVolatile(isVolatile);
1161 setAlignment(0);
JF Bastien800f87a2016-04-06 21:19:33 +00001162 setAtomic(AtomicOrdering::NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001163 AssertOK();
1164 if (Name && Name[0]) setName(Name);
1165}
1166
Christopher Lamb84485702007-04-22 19:24:39 +00001167void LoadInst::setAlignment(unsigned Align) {
1168 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001169 assert(Align <= MaximumAlignment &&
1170 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001171 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001172 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001173 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001174}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001175
1176//===----------------------------------------------------------------------===//
1177// StoreInst Implementation
1178//===----------------------------------------------------------------------===//
1179
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001180void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001181 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001182 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001183 "Ptr must have pointer type!");
1184 assert(getOperand(0)->getType() ==
1185 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001186 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001187 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001188 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001189}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001190
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001191StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001192 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001193
1194StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001195 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001196
Misha Brukmanb1c93172005-04-21 23:48:37 +00001197StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001198 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001199 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001200
1201StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001202 BasicBlock *InsertAtEnd)
1203 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1204
1205StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1206 Instruction *InsertBefore)
JF Bastien800f87a2016-04-06 21:19:33 +00001207 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001208 SyncScope::System, InsertBefore) {}
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001209
1210StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1211 BasicBlock *InsertAtEnd)
JF Bastien800f87a2016-04-06 21:19:33 +00001212 : StoreInst(val, addr, isVolatile, Align, AtomicOrdering::NotAtomic,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001213 SyncScope::System, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001214
Misha Brukmanb1c93172005-04-21 23:48:37 +00001215StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001216 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001217 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001218 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001219 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001220 OperandTraits<StoreInst>::op_begin(this),
1221 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001222 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001223 Op<0>() = val;
1224 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001225 setVolatile(isVolatile);
1226 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001227 setAtomic(Order, SSID);
Dan Gohman68659282007-07-18 20:51:11 +00001228 AssertOK();
1229}
1230
1231StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001232 unsigned Align, AtomicOrdering Order,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001233 SyncScope::ID SSID,
Eli Friedman59b66882011-08-09 23:02:53 +00001234 BasicBlock *InsertAtEnd)
1235 : Instruction(Type::getVoidTy(val->getContext()), Store,
1236 OperandTraits<StoreInst>::op_begin(this),
1237 OperandTraits<StoreInst>::operands(this),
1238 InsertAtEnd) {
1239 Op<0>() = val;
1240 Op<1>() = addr;
1241 setVolatile(isVolatile);
1242 setAlignment(Align);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001243 setAtomic(Order, SSID);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001244 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001245}
1246
Christopher Lamb84485702007-04-22 19:24:39 +00001247void StoreInst::setAlignment(unsigned Align) {
1248 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001249 assert(Align <= MaximumAlignment &&
1250 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001251 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001252 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001253 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001254}
1255
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001256//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001257// AtomicCmpXchgInst Implementation
1258//===----------------------------------------------------------------------===//
1259
1260void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001261 AtomicOrdering SuccessOrdering,
1262 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001263 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001264 Op<0>() = Ptr;
1265 Op<1>() = Cmp;
1266 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001267 setSuccessOrdering(SuccessOrdering);
1268 setFailureOrdering(FailureOrdering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001269 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001270
1271 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1272 "All operands must be non-null!");
1273 assert(getOperand(0)->getType()->isPointerTy() &&
1274 "Ptr must have pointer type!");
1275 assert(getOperand(1)->getType() ==
1276 cast<PointerType>(getOperand(0)->getType())->getElementType()
1277 && "Ptr must be a pointer to Cmp type!");
1278 assert(getOperand(2)->getType() ==
1279 cast<PointerType>(getOperand(0)->getType())->getElementType()
1280 && "Ptr must be a pointer to NewVal type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001281 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001282 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001283 assert(FailureOrdering != AtomicOrdering::NotAtomic &&
Tim Northovere94a5182014-03-11 10:48:52 +00001284 "AtomicCmpXchg instructions must be atomic!");
JF Bastien800f87a2016-04-06 21:19:33 +00001285 assert(!isStrongerThan(FailureOrdering, SuccessOrdering) &&
1286 "AtomicCmpXchg failure argument shall be no stronger than the success "
1287 "argument");
1288 assert(FailureOrdering != AtomicOrdering::Release &&
1289 FailureOrdering != AtomicOrdering::AcquireRelease &&
Tim Northovere94a5182014-03-11 10:48:52 +00001290 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001291}
1292
1293AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001294 AtomicOrdering SuccessOrdering,
1295 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001296 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001297 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001298 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001299 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001300 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1301 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001302 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001303}
1304
1305AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001306 AtomicOrdering SuccessOrdering,
1307 AtomicOrdering FailureOrdering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001308 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001309 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001310 : Instruction(
Serge Gueltone38003f2017-05-09 19:31:13 +00001311 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
Tim Northover420a2162014-06-13 14:24:07 +00001312 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1313 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001314 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001315}
Tim Northover420a2162014-06-13 14:24:07 +00001316
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001317//===----------------------------------------------------------------------===//
1318// AtomicRMWInst Implementation
1319//===----------------------------------------------------------------------===//
1320
1321void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1322 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001323 SyncScope::ID SSID) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001324 Op<0>() = Ptr;
1325 Op<1>() = Val;
1326 setOperation(Operation);
1327 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001328 setSyncScopeID(SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001329
1330 assert(getOperand(0) && getOperand(1) &&
1331 "All operands must be non-null!");
1332 assert(getOperand(0)->getType()->isPointerTy() &&
1333 "Ptr must have pointer type!");
1334 assert(getOperand(1)->getType() ==
1335 cast<PointerType>(getOperand(0)->getType())->getElementType()
1336 && "Ptr must be a pointer to Val type!");
JF Bastien800f87a2016-04-06 21:19:33 +00001337 assert(Ordering != AtomicOrdering::NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001338 "AtomicRMW instructions must be atomic!");
1339}
1340
1341AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1342 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001343 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001344 Instruction *InsertBefore)
1345 : Instruction(Val->getType(), AtomicRMW,
1346 OperandTraits<AtomicRMWInst>::op_begin(this),
1347 OperandTraits<AtomicRMWInst>::operands(this),
1348 InsertBefore) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001349 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001350}
1351
1352AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1353 AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001354 SyncScope::ID SSID,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001355 BasicBlock *InsertAtEnd)
1356 : Instruction(Val->getType(), AtomicRMW,
1357 OperandTraits<AtomicRMWInst>::op_begin(this),
1358 OperandTraits<AtomicRMWInst>::operands(this),
1359 InsertAtEnd) {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001360 Init(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001361}
1362
1363//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001364// FenceInst Implementation
1365//===----------------------------------------------------------------------===//
1366
1367FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001368 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001369 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001370 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001371 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001372 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001373}
1374
1375FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001376 SyncScope::ID SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001377 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001378 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001379 setOrdering(Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001380 setSyncScopeID(SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00001381}
1382
1383//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001384// GetElementPtrInst Implementation
1385//===----------------------------------------------------------------------===//
1386
Jay Foadd1b78492011-07-25 09:48:08 +00001387void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001388 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001389 assert(getNumOperands() == 1 + IdxList.size() &&
1390 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001391 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001392 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001393 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001394}
1395
Gabor Greiff6caff662008-05-10 08:32:32 +00001396GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001397 : Instruction(GEPI.getType(), GetElementPtr,
1398 OperandTraits<GetElementPtrInst>::op_end(this) -
1399 GEPI.getNumOperands(),
1400 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001401 SourceElementType(GEPI.SourceElementType),
1402 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001403 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001404 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001405}
1406
Chris Lattner6090a422009-03-09 04:46:40 +00001407/// getIndexedType - Returns the type of the element that would be accessed with
1408/// a gep instruction with the specified parameters.
1409///
1410/// The Idxs pointer should point to a continuous piece of memory containing the
1411/// indices, either as Value* or uint64_t.
1412///
1413/// A null type is returned if the indices are invalid for the specified
1414/// pointer type.
1415///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001416template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001417static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001418 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001419 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001420 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001421
Chris Lattner6090a422009-03-09 04:46:40 +00001422 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001423 // it cannot be 'stepped over'.
1424 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001425 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001426
Dan Gohman1ecaf452008-05-31 00:58:22 +00001427 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001428 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001429 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001430 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001431 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001432 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001433 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001434 }
Craig Topperc6207612014-04-09 06:08:46 +00001435 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001436}
1437
David Blaikied288fb82015-03-30 21:41:43 +00001438Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1439 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001440}
1441
David Blaikied288fb82015-03-30 21:41:43 +00001442Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001443 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001444 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001445}
1446
David Blaikied288fb82015-03-30 21:41:43 +00001447Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1448 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001449}
1450
Chris Lattner45f15572007-04-14 00:12:57 +00001451/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1452/// zeros. If so, the result pointer and the first operand have the same
1453/// value, just potentially different types.
1454bool GetElementPtrInst::hasAllZeroIndices() const {
1455 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1456 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1457 if (!CI->isZero()) return false;
1458 } else {
1459 return false;
1460 }
1461 }
1462 return true;
1463}
1464
Chris Lattner27058292007-04-27 20:35:56 +00001465/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1466/// constant integers. If so, the result pointer and the first operand have
1467/// a constant offset between them.
1468bool GetElementPtrInst::hasAllConstantIndices() const {
1469 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1470 if (!isa<ConstantInt>(getOperand(i)))
1471 return false;
1472 }
1473 return true;
1474}
1475
Dan Gohman1b849082009-09-07 23:54:19 +00001476void GetElementPtrInst::setIsInBounds(bool B) {
1477 cast<GEPOperator>(this)->setIsInBounds(B);
1478}
Chris Lattner45f15572007-04-14 00:12:57 +00001479
Nick Lewycky28a5f252009-09-27 21:33:04 +00001480bool GetElementPtrInst::isInBounds() const {
1481 return cast<GEPOperator>(this)->isInBounds();
1482}
1483
Chandler Carruth1e140532012-12-11 10:29:10 +00001484bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1485 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001486 // Delegate to the generic GEPOperator implementation.
1487 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001488}
1489
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001490//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001491// ExtractElementInst Implementation
1492//===----------------------------------------------------------------------===//
1493
1494ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001495 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001496 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001497 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001498 ExtractElement,
1499 OperandTraits<ExtractElementInst>::op_begin(this),
1500 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001501 assert(isValidOperands(Val, Index) &&
1502 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001503 Op<0>() = Val;
1504 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001505 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001506}
1507
1508ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001509 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001510 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001511 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001512 ExtractElement,
1513 OperandTraits<ExtractElementInst>::op_begin(this),
1514 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001515 assert(isValidOperands(Val, Index) &&
1516 "Invalid extractelement instruction operands!");
1517
Gabor Greif2d3024d2008-05-26 21:33:52 +00001518 Op<0>() = Val;
1519 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001520 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001521}
1522
Chris Lattner54865b32006-04-08 04:05:48 +00001523bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001524 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001525 return false;
1526 return true;
1527}
1528
Robert Bocchino23004482006-01-10 19:05:34 +00001529//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001530// InsertElementInst Implementation
1531//===----------------------------------------------------------------------===//
1532
Chris Lattner54865b32006-04-08 04:05:48 +00001533InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001534 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001535 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001536 : Instruction(Vec->getType(), InsertElement,
1537 OperandTraits<InsertElementInst>::op_begin(this),
1538 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001539 assert(isValidOperands(Vec, Elt, Index) &&
1540 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001541 Op<0>() = Vec;
1542 Op<1>() = Elt;
1543 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001544 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001545}
1546
Chris Lattner54865b32006-04-08 04:05:48 +00001547InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001548 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001549 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001550 : Instruction(Vec->getType(), InsertElement,
1551 OperandTraits<InsertElementInst>::op_begin(this),
1552 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001553 assert(isValidOperands(Vec, Elt, Index) &&
1554 "Invalid insertelement instruction operands!");
1555
Gabor Greif2d3024d2008-05-26 21:33:52 +00001556 Op<0>() = Vec;
1557 Op<1>() = Elt;
1558 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001559 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001560}
1561
Chris Lattner54865b32006-04-08 04:05:48 +00001562bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1563 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001564 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001565 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001566
Reid Spencerd84d35b2007-02-15 02:26:10 +00001567 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001568 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001569
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001570 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001571 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001572 return true;
1573}
1574
Robert Bocchinoca27f032006-01-17 20:07:22 +00001575//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001576// ShuffleVectorInst Implementation
1577//===----------------------------------------------------------------------===//
1578
1579ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001580 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001581 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001582: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001583 cast<VectorType>(Mask->getType())->getNumElements()),
1584 ShuffleVector,
1585 OperandTraits<ShuffleVectorInst>::op_begin(this),
1586 OperandTraits<ShuffleVectorInst>::operands(this),
1587 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001588 assert(isValidOperands(V1, V2, Mask) &&
1589 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001590 Op<0>() = V1;
1591 Op<1>() = V2;
1592 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001593 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001594}
1595
1596ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001597 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001598 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001599: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1600 cast<VectorType>(Mask->getType())->getNumElements()),
1601 ShuffleVector,
1602 OperandTraits<ShuffleVectorInst>::op_begin(this),
1603 OperandTraits<ShuffleVectorInst>::operands(this),
1604 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001605 assert(isValidOperands(V1, V2, Mask) &&
1606 "Invalid shuffle vector instruction operands!");
1607
Gabor Greif2d3024d2008-05-26 21:33:52 +00001608 Op<0>() = V1;
1609 Op<1>() = V2;
1610 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001611 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001612}
1613
Mon P Wang25f01062008-11-10 04:46:22 +00001614bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001615 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001616 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001617 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001618 return false;
1619
Chris Lattner1dcb6542012-01-25 23:49:49 +00001620 // Mask must be vector of i32.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001621 auto *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001622 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001623 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001624
1625 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001626 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1627 return true;
1628
Sanjay Patel8bd52282017-04-19 16:22:19 +00001629 if (const auto *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001630 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001631 for (Value *Op : MV->operands()) {
Sanjay Patel8bd52282017-04-19 16:22:19 +00001632 if (auto *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001633 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001634 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001635 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001636 return false;
1637 }
1638 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001639 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001640 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001641
Sanjay Patel8bd52282017-04-19 16:22:19 +00001642 if (const auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001643 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1644 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1645 if (CDS->getElementAsInteger(i) >= V1Size*2)
1646 return false;
1647 return true;
1648 }
1649
1650 // The bitcode reader can create a place holder for a forward reference
1651 // used as the shuffle mask. When this occurs, the shuffle mask will
1652 // fall into this case and fail. To avoid this error, do this bit of
1653 // ugliness to allow such a mask pass.
Sanjay Patel8bd52282017-04-19 16:22:19 +00001654 if (const auto *CE = dyn_cast<ConstantExpr>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001655 if (CE->getOpcode() == Instruction::UserOp1)
1656 return true;
1657
1658 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001659}
1660
Chris Lattnercf129702012-01-26 02:51:13 +00001661int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1662 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
Sanjay Patel8bd52282017-04-19 16:22:19 +00001663 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001664 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001665 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001666 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001667 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001668 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001669}
1670
Chris Lattnercf129702012-01-26 02:51:13 +00001671void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1672 SmallVectorImpl<int> &Result) {
1673 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001674
Sanjay Patel8bd52282017-04-19 16:22:19 +00001675 if (auto *CDS = dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001676 for (unsigned i = 0; i != NumElts; ++i)
1677 Result.push_back(CDS->getElementAsInteger(i));
1678 return;
1679 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001680 for (unsigned i = 0; i != NumElts; ++i) {
1681 Constant *C = Mask->getAggregateElement(i);
1682 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001683 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001684 }
1685}
1686
Dan Gohman12fce772008-05-15 19:50:34 +00001687//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001688// InsertValueInst Class
1689//===----------------------------------------------------------------------===//
1690
Jay Foad57aa6362011-07-13 10:26:04 +00001691void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1692 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001693 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001694
1695 // There's no fundamental reason why we require at least one index
1696 // (other than weirdness with &*IdxBegin being invalid; see
1697 // getelementptr's init routine for example). But there's no
1698 // present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001699 assert(!Idxs.empty() && "InsertValueInst must have at least one index");
Jay Foad57aa6362011-07-13 10:26:04 +00001700
1701 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001702 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001703 Op<0>() = Agg;
1704 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001705
Jay Foad57aa6362011-07-13 10:26:04 +00001706 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001707 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001708}
1709
1710InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001711 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001712 OperandTraits<InsertValueInst>::op_begin(this), 2),
1713 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001714 Op<0>() = IVI.getOperand(0);
1715 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001716 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001717}
1718
1719//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001720// ExtractValueInst Class
1721//===----------------------------------------------------------------------===//
1722
Jay Foad57aa6362011-07-13 10:26:04 +00001723void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001724 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001725
Jay Foad57aa6362011-07-13 10:26:04 +00001726 // There's no fundamental reason why we require at least one index.
1727 // But there's no present need to support it.
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00001728 assert(!Idxs.empty() && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001729
Jay Foad57aa6362011-07-13 10:26:04 +00001730 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001731 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001732}
1733
1734ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001735 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001736 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001737 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001738}
1739
Dan Gohman12fce772008-05-15 19:50:34 +00001740// getIndexedType - Returns the type of the element that would be extracted
1741// with an extractvalue instruction with the specified parameters.
1742//
1743// A null type is returned if the indices are invalid for the specified
1744// pointer type.
1745//
Chris Lattner229907c2011-07-18 04:54:35 +00001746Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001747 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001748 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001749 // We can't use CompositeType::indexValid(Index) here.
1750 // indexValid() always returns true for arrays because getelementptr allows
1751 // out-of-bounds indices. Since we don't allow those for extractvalue and
1752 // insertvalue we need to check array indexing manually.
1753 // Since the only other types we can index into are struct types it's just
1754 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001755 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001756 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001757 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001758 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001759 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001760 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001761 } else {
1762 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001763 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001764 }
1765
1766 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001767 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001768 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001769}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001770
1771//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001772// BinaryOperator Class
1773//===----------------------------------------------------------------------===//
1774
Chris Lattner2195fc42007-02-24 00:55:48 +00001775BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001776 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001777 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001778 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001779 OperandTraits<BinaryOperator>::op_begin(this),
1780 OperandTraits<BinaryOperator>::operands(this),
1781 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001782 Op<0>() = S1;
1783 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001784 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00001785 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00001786}
1787
1788BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001789 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001790 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001791 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001792 OperandTraits<BinaryOperator>::op_begin(this),
1793 OperandTraits<BinaryOperator>::operands(this),
1794 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001795 Op<0>() = S1;
1796 Op<1>() = S2;
Chris Lattner2195fc42007-02-24 00:55:48 +00001797 setName(Name);
Craig Topper700892f2017-06-26 07:15:59 +00001798 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +00001799}
1800
Craig Topper700892f2017-06-26 07:15:59 +00001801void BinaryOperator::AssertOK() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001802 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001803 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001804 assert(LHS->getType() == RHS->getType() &&
1805 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001806#ifndef NDEBUG
Craig Topper700892f2017-06-26 07:15:59 +00001807 switch (getOpcode()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001808 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001809 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001810 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001811 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001812 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001813 "Tried to create an integer operation on a non-integer type!");
1814 break;
1815 case FAdd: case FSub:
1816 case FMul:
1817 assert(getType() == LHS->getType() &&
1818 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001819 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001820 "Tried to create a floating-point operation on a "
1821 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001822 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001823 case UDiv:
1824 case SDiv:
1825 assert(getType() == LHS->getType() &&
1826 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001827 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001828 "Incorrect operand type (not integer) for S/UDIV");
1829 break;
1830 case FDiv:
1831 assert(getType() == LHS->getType() &&
1832 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001833 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001834 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001835 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001836 case URem:
1837 case SRem:
1838 assert(getType() == LHS->getType() &&
1839 "Arithmetic operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001840 assert(getType()->isIntOrIntVectorTy() &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001841 "Incorrect operand type (not integer) for S/UREM");
1842 break;
1843 case FRem:
1844 assert(getType() == LHS->getType() &&
1845 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001846 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001847 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001848 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001849 case Shl:
1850 case LShr:
1851 case AShr:
1852 assert(getType() == LHS->getType() &&
1853 "Shift operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001854 assert(getType()->isIntOrIntVectorTy() &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001855 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001856 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001857 case And: case Or:
1858 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001859 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001860 "Logical operation should return same type as operands!");
Craig Topperd1fbb382017-06-25 17:33:48 +00001861 assert(getType()->isIntOrIntVectorTy() &&
Misha Brukman3852f652005-01-27 06:46:38 +00001862 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001863 break;
Craig Topper700892f2017-06-26 07:15:59 +00001864 default: llvm_unreachable("Invalid opcode provided");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001865 }
1866#endif
1867}
1868
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001869BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001870 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001871 Instruction *InsertBefore) {
1872 assert(S1->getType() == S2->getType() &&
1873 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001874 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001875}
1876
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001877BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001878 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001879 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001880 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001881 InsertAtEnd->getInstList().push_back(Res);
1882 return Res;
1883}
1884
Dan Gohman5476cfd2009-08-12 16:23:25 +00001885BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001886 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001887 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001888 return new BinaryOperator(Instruction::Sub,
1889 zero, Op,
1890 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001891}
1892
Dan Gohman5476cfd2009-08-12 16:23:25 +00001893BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001894 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001895 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001896 return new BinaryOperator(Instruction::Sub,
1897 zero, Op,
1898 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001899}
1900
Dan Gohman4ab44202009-12-18 02:58:50 +00001901BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1902 Instruction *InsertBefore) {
1903 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1904 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1905}
1906
1907BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1908 BasicBlock *InsertAtEnd) {
1909 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1910 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1911}
1912
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001913BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1914 Instruction *InsertBefore) {
1915 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1916 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1917}
1918
1919BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1920 BasicBlock *InsertAtEnd) {
1921 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1922 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1923}
1924
Dan Gohman5476cfd2009-08-12 16:23:25 +00001925BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001926 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001927 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001928 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001929 Op->getType(), Name, InsertBefore);
1930}
1931
Dan Gohman5476cfd2009-08-12 16:23:25 +00001932BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001933 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001934 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00001935 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00001936 Op->getType(), Name, InsertAtEnd);
1937}
1938
Dan Gohman5476cfd2009-08-12 16:23:25 +00001939BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001940 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001941 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001942 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001943 Op->getType(), Name, InsertBefore);
1944}
1945
Dan Gohman5476cfd2009-08-12 16:23:25 +00001946BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001947 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00001948 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001949 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001950 Op->getType(), Name, InsertAtEnd);
1951}
1952
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001953// isConstantAllOnes - Helper function for several functions below
1954static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00001955 if (const Constant *C = dyn_cast<Constant>(V))
1956 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00001957 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001958}
1959
Owen Andersonbb2501b2009-07-13 22:18:28 +00001960bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001961 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1962 if (Bop->getOpcode() == Instruction::Sub)
Ana Pazosb3596022016-02-03 21:34:39 +00001963 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0)))
Owen Andersonbb2501b2009-07-13 22:18:28 +00001964 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001965 return false;
1966}
1967
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001968bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001969 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1970 if (Bop->getOpcode() == Instruction::FSub)
Ana Pazosb3596022016-02-03 21:34:39 +00001971 if (Constant *C = dyn_cast<Constant>(Bop->getOperand(0))) {
Shuxin Yangf0537ab2013-01-09 00:13:41 +00001972 if (!IgnoreZeroSign)
1973 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
1974 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
1975 }
Dan Gohmana5b96452009-06-04 22:49:04 +00001976 return false;
1977}
1978
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001979bool BinaryOperator::isNot(const Value *V) {
1980 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1981 return (Bop->getOpcode() == Instruction::Xor &&
1982 (isConstantAllOnes(Bop->getOperand(1)) ||
1983 isConstantAllOnes(Bop->getOperand(0))));
1984 return false;
1985}
1986
Chris Lattner2c7d1772005-04-24 07:28:37 +00001987Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001988 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001989}
1990
Chris Lattner2c7d1772005-04-24 07:28:37 +00001991const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1992 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001993}
1994
Dan Gohmana5b96452009-06-04 22:49:04 +00001995Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001996 return cast<BinaryOperator>(BinOp)->getOperand(1);
1997}
1998
1999const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2000 return getFNegArgument(const_cast<Value*>(BinOp));
2001}
2002
Chris Lattner2c7d1772005-04-24 07:28:37 +00002003Value *BinaryOperator::getNotArgument(Value *BinOp) {
2004 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2005 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2006 Value *Op0 = BO->getOperand(0);
2007 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002008 if (isConstantAllOnes(Op0)) return Op1;
2009
2010 assert(isConstantAllOnes(Op1));
2011 return Op0;
2012}
2013
Chris Lattner2c7d1772005-04-24 07:28:37 +00002014const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2015 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002016}
2017
Sanjay Patel4ce99d42016-11-16 18:09:44 +00002018// Exchange the two operands to this instruction. This instruction is safe to
2019// use on any binary instruction and does not modify the semantics of the
2020// instruction. If the instruction is order-dependent (SetLT f.e.), the opcode
2021// is changed.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002022bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002023 if (!isCommutative())
2024 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002025 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002026 return false;
2027}
2028
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002029//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002030// FPMathOperator Class
2031//===----------------------------------------------------------------------===//
2032
Duncan Sands05f4df82012-04-16 16:28:59 +00002033float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002034 const MDNode *MD =
2035 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002036 if (!MD)
2037 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002038 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002039 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002040}
2041
Duncan Sands05f4df82012-04-16 16:28:59 +00002042//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002043// CastInst Class
2044//===----------------------------------------------------------------------===//
2045
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046// Just determine if this cast only deals with integral->integral conversion.
2047bool CastInst::isIntegerCast() const {
2048 switch (getOpcode()) {
2049 default: return false;
2050 case Instruction::ZExt:
2051 case Instruction::SExt:
2052 case Instruction::Trunc:
2053 return true;
2054 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002055 return getOperand(0)->getType()->isIntegerTy() &&
2056 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002057 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002058}
2059
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002060bool CastInst::isLosslessCast() const {
2061 // Only BitCast can be lossless, exit fast if we're not BitCast
2062 if (getOpcode() != Instruction::BitCast)
2063 return false;
2064
2065 // Identity cast is always lossless
Ana Pazosb3596022016-02-03 21:34:39 +00002066 Type *SrcTy = getOperand(0)->getType();
2067 Type *DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 if (SrcTy == DstTy)
2069 return true;
2070
Reid Spencer8d9336d2006-12-31 05:26:44 +00002071 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002072 if (SrcTy->isPointerTy())
2073 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074 return false; // Other types have no identity values
2075}
2076
2077/// This function determines if the CastInst does not require any bits to be
2078/// changed in order to effect the cast. Essentially, it identifies cases where
2079/// no code gen is necessary for the cast, hence the name no-op cast. For
2080/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002081/// # bitcast i32* %x to i8*
2082/// # bitcast <2 x i32> %x to <4 x i16>
2083/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002084/// @brief Determine if the described cast is a no-op.
2085bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002086 Type *SrcTy,
2087 Type *DestTy,
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002088 const DataLayout &DL) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002089 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002090 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091 case Instruction::Trunc:
2092 case Instruction::ZExt:
2093 case Instruction::SExt:
2094 case Instruction::FPTrunc:
2095 case Instruction::FPExt:
2096 case Instruction::UIToFP:
2097 case Instruction::SIToFP:
2098 case Instruction::FPToUI:
2099 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002100 case Instruction::AddrSpaceCast:
2101 // TODO: Target informations may give a more accurate answer here.
2102 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103 case Instruction::BitCast:
2104 return true; // BitCast never modifies bits.
2105 case Instruction::PtrToInt:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002106 return DL.getIntPtrType(SrcTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002107 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002108 case Instruction::IntToPtr:
Mikael Holmen0ec1d252017-10-05 07:07:09 +00002109 return DL.getIntPtrType(DestTy)->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002110 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002111 }
2112}
2113
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002114bool CastInst::isNoopCast(const DataLayout &DL) const {
Mikael Holmen6efe5072017-10-03 06:03:49 +00002115 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), DL);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002116}
2117
2118/// This function determines if a pair of casts can be eliminated and what
2119/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002120/// instructions like this:
2121/// * %F = firstOpcode SrcTy %x to MidTy
2122/// * %S = secondOpcode MidTy %F to DstTy
2123/// The function returns a resultOpcode so these two casts can be replaced with:
2124/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002125/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002126unsigned CastInst::isEliminableCastPair(
2127 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002128 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2129 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002130 // Define the 144 possibilities for these two cast instructions. The values
2131 // in this matrix determine what to do in a given situation and select the
2132 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002133 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002134 // the following cast properties:
2135 //
2136 // Size Compare Source Destination
2137 // Operator Src ? Size Type Sign Type Sign
2138 // -------- ------------ ------------------- ---------------------
2139 // TRUNC > Integer Any Integral Any
2140 // ZEXT < Integral Unsigned Integer Any
2141 // SEXT < Integral Signed Integer Any
2142 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002143 // FPTOSI n/a FloatPt n/a Integral Signed
2144 // UITOFP n/a Integral Unsigned FloatPt n/a
2145 // SITOFP n/a Integral Signed FloatPt n/a
2146 // FPTRUNC > FloatPt n/a FloatPt n/a
2147 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002148 // PTRTOINT n/a Pointer n/a Integral Unsigned
2149 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002150 // BITCAST = FirstClass n/a FirstClass n/a
2151 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002152 //
2153 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002154 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2155 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002156 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002157 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002158 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002159 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002160 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002161 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2162 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002163 // T F F U S F F P I B A -+
2164 // R Z S P P I I T P 2 N T S |
2165 // U E E 2 2 2 2 R E I T C C +- secondOp
2166 // N X X U S F F N X N 2 V V |
2167 // C T T I I P P C T T P T T -+
2168 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002169 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002170 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2171 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2172 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2173 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2174 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002175 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Craig Topper18799f42018-03-02 18:16:51 +00002176 { 99,99,99, 2, 2,99,99, 8, 2,99,99, 4, 0}, // FPExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002177 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2178 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2179 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2180 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002181 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002182
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002183 // TODO: This logic could be encoded into the table above and handled in the
2184 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002185 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002186 // merging. However, any pair of bitcasts are allowed.
2187 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2188 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2189 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002190
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002191 // Check if any of the casts convert scalars <-> vectors.
2192 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2193 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2194 if (!AreBothBitcasts)
2195 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196
2197 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2198 [secondOp-Instruction::CastOpsBegin];
2199 switch (ElimCase) {
2200 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002201 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202 return 0;
2203 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002204 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002205 return firstOp;
2206 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002207 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002208 return secondOp;
2209 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002210 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002211 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002212 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002213 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002214 return firstOp;
2215 return 0;
2216 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002217 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002218 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002219 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002220 return firstOp;
2221 return 0;
2222 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002223 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002224 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002225 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226 return secondOp;
2227 return 0;
2228 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002229 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002230 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002231 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232 return secondOp;
2233 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002234 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002235 // Cannot simplify if address spaces are different!
2236 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2237 return 0;
2238
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002239 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002240 // We can still fold this without knowing the actual sizes as long we
2241 // know that the intermediate pointer is the largest possible
2242 // pointer size.
2243 // FIXME: Is this always true?
2244 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002245 return Instruction::BitCast;
2246
2247 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002248 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002249 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002250 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002251 if (MidSize >= PtrSize)
2252 return Instruction::BitCast;
2253 return 0;
2254 }
2255 case 8: {
2256 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2257 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2258 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002259 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2260 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002261 if (SrcSize == DstSize)
2262 return Instruction::BitCast;
2263 else if (SrcSize < DstSize)
2264 return firstOp;
2265 return secondOp;
2266 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002267 case 9:
2268 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002269 return Instruction::ZExt;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002270 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002271 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002272 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002273 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002274 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002275 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2276 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002277 if (SrcSize <= PtrSize && SrcSize == DstSize)
2278 return Instruction::BitCast;
2279 return 0;
2280 }
Eugene Zelenkod761e2c2017-05-15 21:57:41 +00002281 case 12:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002282 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2283 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2284 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2285 return Instruction::AddrSpaceCast;
2286 return Instruction::BitCast;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002287 case 13:
2288 // FIXME: this state can be merged with (1), but the following assert
2289 // is useful to check the correcteness of the sequence due to semantic
2290 // change of bitcast.
2291 assert(
2292 SrcTy->isPtrOrPtrVectorTy() &&
2293 MidTy->isPtrOrPtrVectorTy() &&
2294 DstTy->isPtrOrPtrVectorTy() &&
2295 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2296 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2297 "Illegal addrspacecast, bitcast sequence!");
2298 // Allowed, use first cast's opcode
2299 return firstOp;
2300 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002301 // bitcast, addrspacecast -> addrspacecast if the element type of
2302 // bitcast's source is the same as that of addrspacecast's destination.
Peter Collingbourne9d5fd4d2016-11-13 06:58:45 +00002303 if (SrcTy->getScalarType()->getPointerElementType() ==
2304 DstTy->getScalarType()->getPointerElementType())
Jingyue Wu77145d92014-06-06 21:52:55 +00002305 return Instruction::AddrSpaceCast;
2306 return 0;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002307 case 15:
2308 // FIXME: this state can be merged with (1), but the following assert
2309 // is useful to check the correcteness of the sequence due to semantic
2310 // change of bitcast.
2311 assert(
2312 SrcTy->isIntOrIntVectorTy() &&
2313 MidTy->isPtrOrPtrVectorTy() &&
2314 DstTy->isPtrOrPtrVectorTy() &&
2315 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2316 "Illegal inttoptr, bitcast sequence!");
2317 // Allowed, use first cast's opcode
2318 return firstOp;
2319 case 16:
2320 // FIXME: this state can be merged with (2), but the following assert
2321 // is useful to check the correcteness of the sequence due to semantic
2322 // change of bitcast.
2323 assert(
2324 SrcTy->isPtrOrPtrVectorTy() &&
2325 MidTy->isPtrOrPtrVectorTy() &&
2326 DstTy->isIntOrIntVectorTy() &&
2327 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2328 "Illegal bitcast, ptrtoint sequence!");
2329 // Allowed, use second cast's opcode
2330 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002331 case 17:
2332 // (sitofp (zext x)) -> (uitofp x)
2333 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002334 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002335 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002336 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002337 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002338 default:
Craig Topperc514b542012-02-05 22:14:15 +00002339 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002340 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341}
2342
Chris Lattner229907c2011-07-18 04:54:35 +00002343CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002344 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002345 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346 // Construct and return the appropriate CastInst subclass
2347 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002348 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2349 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2350 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2351 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2352 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2353 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2354 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2355 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2356 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2357 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2358 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2359 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2360 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2361 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002363}
2364
Chris Lattner229907c2011-07-18 04:54:35 +00002365CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002366 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002367 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368 // Construct and return the appropriate CastInst subclass
2369 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002370 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2371 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2372 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2373 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2374 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2375 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2376 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2377 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2378 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2379 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2380 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2381 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2382 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2383 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385}
2386
Chris Lattner229907c2011-07-18 04:54:35 +00002387CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002388 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002389 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002390 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002391 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2392 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002393}
2394
Chris Lattner229907c2011-07-18 04:54:35 +00002395CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002396 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002397 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002398 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002399 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2400 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002401}
2402
Chris Lattner229907c2011-07-18 04:54:35 +00002403CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002404 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002405 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002406 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002407 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2408 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002409}
2410
Chris Lattner229907c2011-07-18 04:54:35 +00002411CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002412 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002413 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002414 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002415 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2416 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002417}
2418
Chris Lattner229907c2011-07-18 04:54:35 +00002419CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002420 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002421 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002422 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002423 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2424 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002425}
2426
Chris Lattner229907c2011-07-18 04:54:35 +00002427CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002428 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002429 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002430 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002431 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2432 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002433}
2434
Chris Lattner229907c2011-07-18 04:54:35 +00002435CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002436 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002437 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002438 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2439 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2440 "Invalid cast");
2441 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002442 assert((!Ty->isVectorTy() ||
2443 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002444 "Invalid cast");
2445
Matt Arsenault065ced92013-07-31 00:17:33 +00002446 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002447 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002448
Matt Arsenault740980e2014-07-14 17:24:35 +00002449 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002450}
2451
2452/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002453CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2454 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002455 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002456 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2457 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002458 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002459 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002460 assert((!Ty->isVectorTy() ||
2461 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002462 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002463
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002464 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002465 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002466
Matt Arsenault740980e2014-07-14 17:24:35 +00002467 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2468}
2469
2470CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2471 Value *S, Type *Ty,
2472 const Twine &Name,
2473 BasicBlock *InsertAtEnd) {
2474 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2475 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2476
2477 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2478 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2479
2480 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2481}
2482
2483CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2484 Value *S, Type *Ty,
2485 const Twine &Name,
2486 Instruction *InsertBefore) {
2487 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2488 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2489
2490 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002491 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2492
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002493 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002494}
2495
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002496CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2497 const Twine &Name,
2498 Instruction *InsertBefore) {
2499 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2500 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2501 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2502 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2503
2504 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2505}
2506
Matt Arsenault740980e2014-07-14 17:24:35 +00002507CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002508 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002509 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002510 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002511 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002512 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2513 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002514 Instruction::CastOps opcode =
2515 (SrcBits == DstBits ? Instruction::BitCast :
2516 (SrcBits > DstBits ? Instruction::Trunc :
2517 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002518 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002519}
2520
Chris Lattner229907c2011-07-18 04:54:35 +00002521CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002522 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002523 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002524 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002525 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002526 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2527 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002528 Instruction::CastOps opcode =
2529 (SrcBits == DstBits ? Instruction::BitCast :
2530 (SrcBits > DstBits ? Instruction::Trunc :
2531 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002532 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002533}
2534
Chris Lattner229907c2011-07-18 04:54:35 +00002535CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002536 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002537 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002538 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002539 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002540 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2541 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002542 Instruction::CastOps opcode =
2543 (SrcBits == DstBits ? Instruction::BitCast :
2544 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002545 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002546}
2547
Chris Lattner229907c2011-07-18 04:54:35 +00002548CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002549 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002550 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002551 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002552 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002553 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2554 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002555 Instruction::CastOps opcode =
2556 (SrcBits == DstBits ? Instruction::BitCast :
2557 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002558 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002559}
2560
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002561// Check whether it is valid to call getCastOpcode for these types.
2562// This routine must be kept in sync with getCastOpcode.
2563bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2564 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2565 return false;
2566
2567 if (SrcTy == DestTy)
2568 return true;
2569
2570 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2571 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2572 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2573 // An element by element cast. Valid if casting the elements is valid.
2574 SrcTy = SrcVecTy->getElementType();
2575 DestTy = DestVecTy->getElementType();
2576 }
2577
2578 // Get the bit sizes, we'll need these
2579 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2580 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2581
2582 // Run through the possibilities ...
2583 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002584 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002585 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002586 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002587 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002588 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002589 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002590 // Casting from something else
2591 return SrcTy->isPointerTy();
2592 }
2593 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2594 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002595 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002596 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002597 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002598 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002599 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002600 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002601 return false;
2602 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002603 if (DestTy->isVectorTy()) // Casting to vector
2604 return DestBits == SrcBits;
2605 if (DestTy->isPointerTy()) { // Casting to pointer
2606 if (SrcTy->isPointerTy()) // Casting from pointer
2607 return true;
2608 return SrcTy->isIntegerTy(); // Casting from integral
2609 }
2610 if (DestTy->isX86_MMXTy()) {
2611 if (SrcTy->isVectorTy())
2612 return DestBits == SrcBits; // 64-bit vector to MMX
2613 return false;
2614 } // Casting to something else
2615 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002616}
2617
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002618bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2619 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2620 return false;
2621
2622 if (SrcTy == DestTy)
2623 return true;
2624
2625 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2626 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2627 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2628 // An element by element cast. Valid if casting the elements is valid.
2629 SrcTy = SrcVecTy->getElementType();
2630 DestTy = DestVecTy->getElementType();
2631 }
2632 }
2633 }
2634
2635 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2636 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2637 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2638 }
2639 }
2640
2641 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2642 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2643
2644 // Could still have vectors of pointers if the number of elements doesn't
2645 // match
2646 if (SrcBits == 0 || DestBits == 0)
2647 return false;
2648
2649 if (SrcBits != DestBits)
2650 return false;
2651
2652 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2653 return false;
2654
2655 return true;
2656}
2657
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002658bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002659 const DataLayout &DL) {
Yichao Yu92c11ee2017-10-22 20:28:17 +00002660 // ptrtoint and inttoptr are not allowed on non-integral pointers
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002661 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2662 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002663 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2664 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002665 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2666 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Yichao Yu92c11ee2017-10-22 20:28:17 +00002667 return (IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy) &&
2668 !DL.isNonIntegralPointerType(PtrTy));
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002669
2670 return isBitCastable(SrcTy, DestTy);
2671}
2672
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002673// Provide a way to get a "cast" where the cast opcode is inferred from the
2674// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002675// logic in the castIsValid function below. This axiom should hold:
2676// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2677// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002679// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002680Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002681CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002682 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2683 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684
Duncan Sands55e50902008-01-06 10:12:28 +00002685 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2686 "Only first class types are castable!");
2687
Duncan Sandsa8514532011-05-18 07:13:41 +00002688 if (SrcTy == DestTy)
2689 return BitCast;
2690
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002691 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002692 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2693 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002694 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2695 // An element by element cast. Find the appropriate opcode based on the
2696 // element types.
2697 SrcTy = SrcVecTy->getElementType();
2698 DestTy = DestVecTy->getElementType();
2699 }
2700
2701 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002702 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2703 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002704
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002705 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002706 if (DestTy->isIntegerTy()) { // Casting to integral
2707 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002708 if (DestBits < SrcBits)
2709 return Trunc; // int -> smaller int
2710 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002711 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002712 return SExt; // signed -> SEXT
2713 else
2714 return ZExt; // unsigned -> ZEXT
2715 } else {
2716 return BitCast; // Same size, No-op cast
2717 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002718 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002719 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002720 return FPToSI; // FP -> sint
2721 else
2722 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002723 } else if (SrcTy->isVectorTy()) {
2724 assert(DestBits == SrcBits &&
2725 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002726 return BitCast; // Same size, no-op cast
2727 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002728 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002729 "Casting from a value that is not first-class type");
2730 return PtrToInt; // ptr -> int
2731 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002732 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2733 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002734 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002735 return SIToFP; // sint -> FP
2736 else
2737 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002738 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002739 if (DestBits < SrcBits) {
2740 return FPTrunc; // FP -> smaller FP
2741 } else if (DestBits > SrcBits) {
2742 return FPExt; // FP -> larger FP
2743 } else {
2744 return BitCast; // same size, no-op cast
2745 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002746 } else if (SrcTy->isVectorTy()) {
2747 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002748 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002749 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002750 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002751 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002752 } else if (DestTy->isVectorTy()) {
2753 assert(DestBits == SrcBits &&
2754 "Illegal cast to vector (wrong type or size)");
2755 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002756 } else if (DestTy->isPointerTy()) {
2757 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002758 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2759 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002760 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002761 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002762 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002763 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002764 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002765 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002766 if (SrcTy->isVectorTy()) {
2767 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002768 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002769 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002770 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002771 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002772 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002773}
2774
2775//===----------------------------------------------------------------------===//
2776// CastInst SubClass Constructors
2777//===----------------------------------------------------------------------===//
2778
2779/// Check that the construction parameters for a CastInst are correct. This
2780/// could be broken out into the separate constructors but it is useful to have
2781/// it in one place and to eliminate the redundant code for getting the sizes
2782/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002783bool
Chris Lattner229907c2011-07-18 04:54:35 +00002784CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002785 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00002786 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00002787
Chris Lattner37bc78a2010-01-26 21:51:43 +00002788 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2789 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002790 return false;
2791
2792 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002793 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2794 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002795
Duncan Sands7f646562011-05-18 09:21:57 +00002796 // If these are vector types, get the lengths of the vectors (using zero for
2797 // scalar types means that checking that vector lengths match also checks that
2798 // scalars are not being converted to vectors or vectors to scalars).
2799 unsigned SrcLength = SrcTy->isVectorTy() ?
2800 cast<VectorType>(SrcTy)->getNumElements() : 0;
2801 unsigned DstLength = DstTy->isVectorTy() ?
2802 cast<VectorType>(DstTy)->getNumElements() : 0;
2803
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002804 // Switch on the opcode provided
2805 switch (op) {
2806 default: return false; // This is an input error
2807 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002808 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2809 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002810 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002811 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2812 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002813 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002814 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2815 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002816 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00002817 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2818 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002819 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00002820 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2821 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002822 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002823 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00002824 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2825 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002826 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002827 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00002828 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2829 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002830 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00002831 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002832 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002833 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2834 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2835 return false;
Craig Topper95d23472017-07-09 07:04:00 +00002836 return SrcTy->isPtrOrPtrVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002837 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00002838 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00002839 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00002840 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2841 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2842 return false;
Craig Topper95d23472017-07-09 07:04:00 +00002843 return SrcTy->isIntOrIntVectorTy() && DstTy->isPtrOrPtrVectorTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002844 case Instruction::BitCast: {
2845 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2846 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2847
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002848 // BitCast implies a no-op cast of type only. No bits change.
2849 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002850 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002851 return false;
2852
Alp Tokerf907b892013-12-05 05:44:44 +00002853 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002854 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002855 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002856 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2857
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002858 // If both are pointers then the address spaces must match.
2859 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
2860 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002861
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00002862 // A vector of pointers must have the same number of elements.
2863 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2864 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2865 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2866
2867 return false;
2868 }
2869
2870 return true;
2871 }
2872 case Instruction::AddrSpaceCast: {
2873 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
2874 if (!SrcPtrTy)
2875 return false;
2876
2877 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
2878 if (!DstPtrTy)
2879 return false;
2880
2881 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
2882 return false;
2883
2884 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2885 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
2886 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
2887
2888 return false;
2889 }
2890
2891 return true;
2892 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002893 }
2894}
2895
2896TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002897 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002898) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002899 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002900}
2901
2902TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002903 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002904) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002905 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002906}
2907
2908ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002909 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002910) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002911 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002912}
2913
2914ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002915 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002916) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002917 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002918}
2919SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002920 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002921) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002922 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002923}
2924
Jeff Cohencc08c832006-12-02 02:22:01 +00002925SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002926 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002927) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002928 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002929}
2930
2931FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002932 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002934 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002935}
2936
2937FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002938 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002939) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002940 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002941}
2942
2943FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002944 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002946 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002947}
2948
2949FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002950 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002952 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002953}
2954
2955UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002956 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002957) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002958 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002959}
2960
2961UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002962 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002963) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002964 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002965}
2966
2967SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002968 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002969) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002970 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002971}
2972
2973SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002974 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002975) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002976 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002977}
2978
2979FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002980 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002981) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002982 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002983}
2984
2985FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002986 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002987) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002988 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002989}
2990
2991FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002992 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002993) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002994 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002995}
2996
2997FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00002998 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002999) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003000 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003001}
3002
3003PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003004 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003005) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003006 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003007}
3008
3009PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003010 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003011) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003012 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003013}
3014
3015IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003016 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003017) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003018 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003019}
3020
3021IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003022 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003023) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003024 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003025}
3026
3027BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003028 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003029) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003030 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003031}
3032
3033BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003034 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003035) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003036 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003037}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003038
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003039AddrSpaceCastInst::AddrSpaceCastInst(
3040 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3041) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3042 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3043}
3044
3045AddrSpaceCastInst::AddrSpaceCastInst(
3046 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3047) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3048 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3049}
3050
Chris Lattnerf16dc002006-09-17 19:29:56 +00003051//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003052// CmpInst Classes
3053//===----------------------------------------------------------------------===//
3054
Craig Topper1c3f2832015-12-15 06:11:33 +00003055CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3056 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003057 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003058 OperandTraits<CmpInst>::op_begin(this),
3059 OperandTraits<CmpInst>::operands(this),
3060 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003061 Op<0>() = LHS;
3062 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003063 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003064 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003065}
Gabor Greiff6caff662008-05-10 08:32:32 +00003066
Craig Topper1c3f2832015-12-15 06:11:33 +00003067CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3068 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003069 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003070 OperandTraits<CmpInst>::op_begin(this),
3071 OperandTraits<CmpInst>::operands(this),
3072 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003073 Op<0>() = LHS;
3074 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003075 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003076 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003077}
3078
3079CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003080CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003081 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003082 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003083 if (InsertBefore)
3084 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3085 S1, S2, Name);
3086 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003087 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003088 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003089 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003090
3091 if (InsertBefore)
3092 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3093 S1, S2, Name);
3094 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003095 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003096 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003097}
3098
3099CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003100CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003101 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003102 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003103 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3104 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003105 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003106 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3107 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003108}
3109
3110void CmpInst::swapOperands() {
3111 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3112 IC->swapOperands();
3113 else
3114 cast<FCmpInst>(this)->swapOperands();
3115}
3116
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003117bool CmpInst::isCommutative() const {
3118 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003119 return IC->isCommutative();
3120 return cast<FCmpInst>(this)->isCommutative();
3121}
3122
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003123bool CmpInst::isEquality() const {
3124 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003125 return IC->isEquality();
3126 return cast<FCmpInst>(this)->isEquality();
3127}
3128
Dan Gohman4e724382008-05-31 02:47:54 +00003129CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003130 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003131 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003132 case ICMP_EQ: return ICMP_NE;
3133 case ICMP_NE: return ICMP_EQ;
3134 case ICMP_UGT: return ICMP_ULE;
3135 case ICMP_ULT: return ICMP_UGE;
3136 case ICMP_UGE: return ICMP_ULT;
3137 case ICMP_ULE: return ICMP_UGT;
3138 case ICMP_SGT: return ICMP_SLE;
3139 case ICMP_SLT: return ICMP_SGE;
3140 case ICMP_SGE: return ICMP_SLT;
3141 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003142
Dan Gohman4e724382008-05-31 02:47:54 +00003143 case FCMP_OEQ: return FCMP_UNE;
3144 case FCMP_ONE: return FCMP_UEQ;
3145 case FCMP_OGT: return FCMP_ULE;
3146 case FCMP_OLT: return FCMP_UGE;
3147 case FCMP_OGE: return FCMP_ULT;
3148 case FCMP_OLE: return FCMP_UGT;
3149 case FCMP_UEQ: return FCMP_ONE;
3150 case FCMP_UNE: return FCMP_OEQ;
3151 case FCMP_UGT: return FCMP_OLE;
3152 case FCMP_ULT: return FCMP_OGE;
3153 case FCMP_UGE: return FCMP_OLT;
3154 case FCMP_ULE: return FCMP_OGT;
3155 case FCMP_ORD: return FCMP_UNO;
3156 case FCMP_UNO: return FCMP_ORD;
3157 case FCMP_TRUE: return FCMP_FALSE;
3158 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003159 }
3160}
3161
Tim Northoverde3aea0412016-08-17 20:25:25 +00003162StringRef CmpInst::getPredicateName(Predicate Pred) {
3163 switch (Pred) {
3164 default: return "unknown";
3165 case FCmpInst::FCMP_FALSE: return "false";
3166 case FCmpInst::FCMP_OEQ: return "oeq";
3167 case FCmpInst::FCMP_OGT: return "ogt";
3168 case FCmpInst::FCMP_OGE: return "oge";
3169 case FCmpInst::FCMP_OLT: return "olt";
3170 case FCmpInst::FCMP_OLE: return "ole";
3171 case FCmpInst::FCMP_ONE: return "one";
3172 case FCmpInst::FCMP_ORD: return "ord";
3173 case FCmpInst::FCMP_UNO: return "uno";
3174 case FCmpInst::FCMP_UEQ: return "ueq";
3175 case FCmpInst::FCMP_UGT: return "ugt";
3176 case FCmpInst::FCMP_UGE: return "uge";
3177 case FCmpInst::FCMP_ULT: return "ult";
3178 case FCmpInst::FCMP_ULE: return "ule";
3179 case FCmpInst::FCMP_UNE: return "une";
3180 case FCmpInst::FCMP_TRUE: return "true";
3181 case ICmpInst::ICMP_EQ: return "eq";
3182 case ICmpInst::ICMP_NE: return "ne";
3183 case ICmpInst::ICMP_SGT: return "sgt";
3184 case ICmpInst::ICMP_SGE: return "sge";
3185 case ICmpInst::ICMP_SLT: return "slt";
3186 case ICmpInst::ICMP_SLE: return "sle";
3187 case ICmpInst::ICMP_UGT: return "ugt";
3188 case ICmpInst::ICMP_UGE: return "uge";
3189 case ICmpInst::ICMP_ULT: return "ult";
3190 case ICmpInst::ICMP_ULE: return "ule";
3191 }
3192}
3193
Reid Spencer266e42b2006-12-23 06:05:41 +00003194ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3195 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003196 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003197 case ICMP_EQ: case ICMP_NE:
3198 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3199 return pred;
3200 case ICMP_UGT: return ICMP_SGT;
3201 case ICMP_ULT: return ICMP_SLT;
3202 case ICMP_UGE: return ICMP_SGE;
3203 case ICMP_ULE: return ICMP_SLE;
3204 }
3205}
3206
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003207ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3208 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003209 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003210 case ICMP_EQ: case ICMP_NE:
3211 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3212 return pred;
3213 case ICMP_SGT: return ICMP_UGT;
3214 case ICMP_SLT: return ICMP_ULT;
3215 case ICMP_SGE: return ICMP_UGE;
3216 case ICMP_SLE: return ICMP_ULE;
3217 }
3218}
3219
Serguei Katkov3cb4c342018-02-09 07:59:07 +00003220CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
3221 switch (pred) {
3222 default: llvm_unreachable("Unknown or unsupported cmp predicate!");
3223 case ICMP_SGT: return ICMP_SGE;
3224 case ICMP_SLT: return ICMP_SLE;
3225 case ICMP_SGE: return ICMP_SGT;
3226 case ICMP_SLE: return ICMP_SLT;
3227 case ICMP_UGT: return ICMP_UGE;
3228 case ICMP_ULT: return ICMP_ULE;
3229 case ICMP_UGE: return ICMP_UGT;
3230 case ICMP_ULE: return ICMP_ULT;
3231
3232 case FCMP_OGT: return FCMP_OGE;
3233 case FCMP_OLT: return FCMP_OLE;
3234 case FCMP_OGE: return FCMP_OGT;
3235 case FCMP_OLE: return FCMP_OLT;
3236 case FCMP_UGT: return FCMP_UGE;
3237 case FCMP_ULT: return FCMP_ULE;
3238 case FCMP_UGE: return FCMP_UGT;
3239 case FCMP_ULE: return FCMP_ULT;
3240 }
3241}
3242
Dan Gohman4e724382008-05-31 02:47:54 +00003243CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003244 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003245 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003246 case ICMP_EQ: case ICMP_NE:
3247 return pred;
3248 case ICMP_SGT: return ICMP_SLT;
3249 case ICMP_SLT: return ICMP_SGT;
3250 case ICMP_SGE: return ICMP_SLE;
3251 case ICMP_SLE: return ICMP_SGE;
3252 case ICMP_UGT: return ICMP_ULT;
3253 case ICMP_ULT: return ICMP_UGT;
3254 case ICMP_UGE: return ICMP_ULE;
3255 case ICMP_ULE: return ICMP_UGE;
3256
Reid Spencerd9436b62006-11-20 01:22:35 +00003257 case FCMP_FALSE: case FCMP_TRUE:
3258 case FCMP_OEQ: case FCMP_ONE:
3259 case FCMP_UEQ: case FCMP_UNE:
3260 case FCMP_ORD: case FCMP_UNO:
3261 return pred;
3262 case FCMP_OGT: return FCMP_OLT;
3263 case FCMP_OLT: return FCMP_OGT;
3264 case FCMP_OGE: return FCMP_OLE;
3265 case FCMP_OLE: return FCMP_OGE;
3266 case FCMP_UGT: return FCMP_ULT;
3267 case FCMP_ULT: return FCMP_UGT;
3268 case FCMP_UGE: return FCMP_ULE;
3269 case FCMP_ULE: return FCMP_UGE;
3270 }
3271}
3272
Max Kazantsevb299ade2018-02-07 11:16:29 +00003273CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
3274 switch (pred) {
3275 case ICMP_SGT: return ICMP_SGE;
3276 case ICMP_SLT: return ICMP_SLE;
3277 case ICMP_UGT: return ICMP_UGE;
3278 case ICMP_ULT: return ICMP_ULE;
3279 case FCMP_OGT: return FCMP_OGE;
3280 case FCMP_OLT: return FCMP_OLE;
3281 case FCMP_UGT: return FCMP_UGE;
3282 case FCMP_ULT: return FCMP_ULE;
3283 default: return pred;
3284 }
3285}
3286
Sanjoy Das6e78b172015-10-22 19:57:34 +00003287CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3288 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3289
3290 switch (pred) {
3291 default:
3292 llvm_unreachable("Unknown predicate!");
3293 case CmpInst::ICMP_ULT:
3294 return CmpInst::ICMP_SLT;
3295 case CmpInst::ICMP_ULE:
3296 return CmpInst::ICMP_SLE;
3297 case CmpInst::ICMP_UGT:
3298 return CmpInst::ICMP_SGT;
3299 case CmpInst::ICMP_UGE:
3300 return CmpInst::ICMP_SGE;
3301 }
3302}
3303
Craig Topper1c3f2832015-12-15 06:11:33 +00003304bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003305 switch (predicate) {
3306 default: return false;
3307 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3308 case ICmpInst::ICMP_UGE: return true;
3309 }
3310}
3311
Craig Topper1c3f2832015-12-15 06:11:33 +00003312bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003313 switch (predicate) {
3314 default: return false;
3315 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3316 case ICmpInst::ICMP_SGE: return true;
3317 }
3318}
3319
Craig Topper1c3f2832015-12-15 06:11:33 +00003320bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003321 switch (predicate) {
3322 default: return false;
3323 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3324 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3325 case FCmpInst::FCMP_ORD: return true;
3326 }
3327}
3328
Craig Topper1c3f2832015-12-15 06:11:33 +00003329bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003330 switch (predicate) {
3331 default: return false;
3332 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3333 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3334 case FCmpInst::FCMP_UNO: return true;
3335 }
3336}
3337
Craig Topper1c3f2832015-12-15 06:11:33 +00003338bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003339 switch(predicate) {
3340 default: return false;
3341 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3342 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3343 }
3344}
3345
Craig Topper1c3f2832015-12-15 06:11:33 +00003346bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003347 switch(predicate) {
3348 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3349 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3350 default: return false;
3351 }
3352}
3353
Chad Rosier99bc4802016-04-21 16:18:02 +00003354bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosieraf83e402016-04-21 14:04:54 +00003355 // If the predicates match, then we know the first condition implies the
3356 // second is true.
3357 if (Pred1 == Pred2)
3358 return true;
3359
3360 switch (Pred1) {
3361 default:
3362 break;
Chad Rosier1a601592016-04-22 17:57:34 +00003363 case ICMP_EQ:
Chad Rosier3d75f8c2016-04-25 13:25:14 +00003364 // 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 +00003365 return Pred2 == ICMP_UGE || Pred2 == ICMP_ULE || Pred2 == ICMP_SGE ||
3366 Pred2 == ICMP_SLE;
Chad Rosier3456cb52016-04-22 17:14:12 +00003367 case ICMP_UGT: // A >u B implies A != B and A >=u B are true.
3368 return Pred2 == ICMP_NE || Pred2 == ICMP_UGE;
3369 case ICMP_ULT: // A <u B implies A != B and A <=u B are true.
3370 return Pred2 == ICMP_NE || Pred2 == ICMP_ULE;
3371 case ICMP_SGT: // A >s B implies A != B and A >=s B are true.
3372 return Pred2 == ICMP_NE || Pred2 == ICMP_SGE;
3373 case ICMP_SLT: // A <s B implies A != B and A <=s B are true.
3374 return Pred2 == ICMP_NE || Pred2 == ICMP_SLE;
Chad Rosieraf83e402016-04-21 14:04:54 +00003375 }
3376 return false;
3377}
3378
Chad Rosier99bc4802016-04-21 16:18:02 +00003379bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
Chad Rosier1a601592016-04-22 17:57:34 +00003380 return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
Chad Rosieraf83e402016-04-21 14:04:54 +00003381}
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003382
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003383//===----------------------------------------------------------------------===//
3384// SwitchInst Implementation
3385//===----------------------------------------------------------------------===//
3386
Chris Lattnerbaf00152010-11-17 05:41:46 +00003387void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3388 assert(Value && Default && NumReserved);
3389 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003390 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003391 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003392
Pete Coopereb31b682015-05-21 22:48:54 +00003393 Op<0>() = Value;
3394 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003395}
3396
Chris Lattner2195fc42007-02-24 00:55:48 +00003397/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3398/// switch on and a default destination. The number of additional cases can
3399/// be specified here to make memory allocation more efficient. This
3400/// constructor can also autoinsert before another instruction.
3401SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3402 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003403 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003404 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003405 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003406}
3407
3408/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3409/// switch on and a default destination. The number of additional cases can
3410/// be specified here to make memory allocation more efficient. This
3411/// constructor also autoinserts at the end of the specified BasicBlock.
3412SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3413 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003414 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003415 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003416 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003417}
3418
Misha Brukmanb1c93172005-04-21 23:48:37 +00003419SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003420 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003421 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003422 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003423 Use *OL = getOperandList();
3424 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003425 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003426 OL[i] = InOL[i];
3427 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003428 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003429 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003430}
3431
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003432
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003433/// addCase - Add an entry to the switch instruction...
3434///
Chris Lattner47ac1872005-02-24 05:32:09 +00003435void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003436 unsigned NewCaseIdx = getNumCases();
3437 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003438 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003439 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003440 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003441 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003442 setNumHungOffUseOperands(OpNo+2);
Chandler Carruth927d8e62017-04-12 07:27:28 +00003443 CaseHandle Case(this, NewCaseIdx);
Bob Wilsone4077362013-09-09 19:14:35 +00003444 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003445 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003446}
3447
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003448/// removeCase - This method removes the specified case and its successor
3449/// from the switch instruction.
Chandler Carruth927d8e62017-04-12 07:27:28 +00003450SwitchInst::CaseIt SwitchInst::removeCase(CaseIt I) {
3451 unsigned idx = I->getCaseIndex();
3452
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003453 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003454
3455 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003456 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003457
Jay Foad14277722011-02-01 09:22:34 +00003458 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003459 if (2 + (idx + 1) * 2 != NumOps) {
3460 OL[2 + idx * 2] = OL[NumOps - 2];
3461 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003462 }
3463
3464 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003465 OL[NumOps-2].set(nullptr);
3466 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003467 setNumHungOffUseOperands(NumOps-2);
Chandler Carruth0d256c02017-03-26 02:49:23 +00003468
3469 return CaseIt(this, idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003470}
3471
Jay Foade98f29d2011-04-01 08:00:58 +00003472/// growOperands - grow operands - This grows the operand list in response
3473/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003474///
Jay Foade98f29d2011-04-01 08:00:58 +00003475void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003476 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003477 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003478
3479 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003480 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003481}
3482
Chris Lattner3ed871f2009-10-27 19:13:16 +00003483//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003484// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003485//===----------------------------------------------------------------------===//
3486
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003487void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003488 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003489 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003490 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003491 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003492 allocHungoffUses(ReservedSpace);
3493
Pete Coopereb31b682015-05-21 22:48:54 +00003494 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003495}
3496
3497
Jay Foade98f29d2011-04-01 08:00:58 +00003498/// growOperands - grow operands - This grows the operand list in response
3499/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003500///
Jay Foade98f29d2011-04-01 08:00:58 +00003501void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003502 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003503 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003504
3505 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003506 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003507}
3508
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003509IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3510 Instruction *InsertBefore)
3511: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003512 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003513 init(Address, NumCases);
3514}
3515
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003516IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3517 BasicBlock *InsertAtEnd)
3518: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003519 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003520 init(Address, NumCases);
3521}
3522
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003523IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003524 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3525 nullptr, IBI.getNumOperands()) {
3526 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003527 Use *OL = getOperandList();
3528 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003529 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3530 OL[i] = InOL[i];
3531 SubclassOptionalData = IBI.SubclassOptionalData;
3532}
3533
Chris Lattner3ed871f2009-10-27 19:13:16 +00003534/// addDestination - Add a destination.
3535///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003536void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003537 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003538 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003539 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003540 // Initialize some new operands.
3541 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003542 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003543 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003544}
3545
3546/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003547/// indirectbr instruction.
3548void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003549 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3550
3551 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003552 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003553
3554 // Replace this value with the last one.
3555 OL[idx+1] = OL[NumOps-1];
3556
3557 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003558 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003559 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003560}
3561
Chris Lattner3ed871f2009-10-27 19:13:16 +00003562//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003563// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003564//===----------------------------------------------------------------------===//
3565
Chris Lattnerf22be932004-10-15 23:52:53 +00003566// Define these methods here so vtables don't get emitted into every translation
3567// unit that uses these classes.
3568
Pete Cooper75403d72015-06-24 20:22:23 +00003569GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003570 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003571}
3572
Pete Cooper75403d72015-06-24 20:22:23 +00003573BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003574 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003575}
3576
Pete Cooper75403d72015-06-24 20:22:23 +00003577FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003578 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003579}
3580
Pete Cooper75403d72015-06-24 20:22:23 +00003581ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003582 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003583}
3584
Pete Cooper75403d72015-06-24 20:22:23 +00003585ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003586 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003587}
3588
Pete Cooper75403d72015-06-24 20:22:23 +00003589InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003590 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003591}
3592
Pete Cooper75403d72015-06-24 20:22:23 +00003593AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003594 AllocaInst *Result = new AllocaInst(getAllocatedType(),
Matt Arsenault3c1fc762017-04-10 22:27:50 +00003595 getType()->getAddressSpace(),
David Majnemer6b3244c2014-04-30 16:12:21 +00003596 (Value *)getOperand(0), getAlignment());
3597 Result->setUsedWithInAlloca(isUsedWithInAlloca());
Manman Ren9bfd0d02016-04-01 21:41:15 +00003598 Result->setSwiftError(isSwiftError());
David Majnemer6b3244c2014-04-30 16:12:21 +00003599 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003600}
3601
Pete Cooper75403d72015-06-24 20:22:23 +00003602LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003603 return new LoadInst(getOperand(0), Twine(), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003604 getAlignment(), getOrdering(), getSyncScopeID());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003605}
3606
Pete Cooper75403d72015-06-24 20:22:23 +00003607StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003608 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003609 getAlignment(), getOrdering(), getSyncScopeID());
Eli Friedman59b66882011-08-09 23:02:53 +00003610
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003611}
3612
Pete Cooper75403d72015-06-24 20:22:23 +00003613AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003614 AtomicCmpXchgInst *Result =
3615 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003616 getSuccessOrdering(), getFailureOrdering(),
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003617 getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003618 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003619 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003620 return Result;
3621}
3622
Pete Cooper75403d72015-06-24 20:22:23 +00003623AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003624 AtomicRMWInst *Result =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003625 new AtomicRMWInst(getOperation(), getOperand(0), getOperand(1),
3626 getOrdering(), getSyncScopeID());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003627 Result->setVolatile(isVolatile());
3628 return Result;
3629}
3630
Pete Cooper75403d72015-06-24 20:22:23 +00003631FenceInst *FenceInst::cloneImpl() const {
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003632 return new FenceInst(getContext(), getOrdering(), getSyncScopeID());
Eli Friedmanfee02c62011-07-25 23:16:38 +00003633}
3634
Pete Cooper75403d72015-06-24 20:22:23 +00003635TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003636 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003637}
3638
Pete Cooper75403d72015-06-24 20:22:23 +00003639ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003640 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003641}
3642
Pete Cooper75403d72015-06-24 20:22:23 +00003643SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003644 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003645}
3646
Pete Cooper75403d72015-06-24 20:22:23 +00003647FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003648 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003649}
3650
Pete Cooper75403d72015-06-24 20:22:23 +00003651FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003652 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003653}
3654
Pete Cooper75403d72015-06-24 20:22:23 +00003655UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003656 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003657}
3658
Pete Cooper75403d72015-06-24 20:22:23 +00003659SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003660 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003661}
3662
Pete Cooper75403d72015-06-24 20:22:23 +00003663FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003664 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003665}
3666
Pete Cooper75403d72015-06-24 20:22:23 +00003667FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003668 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003669}
3670
Pete Cooper75403d72015-06-24 20:22:23 +00003671PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003672 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003673}
3674
Pete Cooper75403d72015-06-24 20:22:23 +00003675IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003676 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003677}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003678
Pete Cooper75403d72015-06-24 20:22:23 +00003679BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003680 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003681}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003682
Pete Cooper75403d72015-06-24 20:22:23 +00003683AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003684 return new AddrSpaceCastInst(getOperand(0), getType());
3685}
3686
Pete Cooper75403d72015-06-24 20:22:23 +00003687CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003688 if (hasOperandBundles()) {
3689 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3690 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3691 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003692 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003693}
3694
Pete Cooper75403d72015-06-24 20:22:23 +00003695SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003696 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003697}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003698
Pete Cooper75403d72015-06-24 20:22:23 +00003699VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003700 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003701}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003702
Pete Cooper75403d72015-06-24 20:22:23 +00003703ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003704 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003705}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003706
Pete Cooper75403d72015-06-24 20:22:23 +00003707InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003708 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003709}
3710
Pete Cooper75403d72015-06-24 20:22:23 +00003711ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003712 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003713}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003714
Pete Cooper75403d72015-06-24 20:22:23 +00003715PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003716
Pete Cooper75403d72015-06-24 20:22:23 +00003717LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003718 return new LandingPadInst(*this);
3719}
3720
Pete Cooper75403d72015-06-24 20:22:23 +00003721ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003722 return new(getNumOperands()) ReturnInst(*this);
3723}
3724
Pete Cooper75403d72015-06-24 20:22:23 +00003725BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003726 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003727}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003728
Pete Cooper75403d72015-06-24 20:22:23 +00003729SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003730
Pete Cooper75403d72015-06-24 20:22:23 +00003731IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003732 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003733}
3734
Pete Cooper75403d72015-06-24 20:22:23 +00003735InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003736 if (hasOperandBundles()) {
3737 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3738 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3739 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003740 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003741}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003742
Pete Cooper75403d72015-06-24 20:22:23 +00003743ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003744
David Majnemer654e1302015-07-31 17:58:14 +00003745CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3746 return new (getNumOperands()) CleanupReturnInst(*this);
3747}
3748
David Majnemer654e1302015-07-31 17:58:14 +00003749CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003750 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003751}
3752
David Majnemer8a1c45d2015-12-12 05:38:55 +00003753CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3754 return new CatchSwitchInst(*this);
3755}
3756
3757FuncletPadInst *FuncletPadInst::cloneImpl() const {
3758 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003759}
3760
Pete Cooper75403d72015-06-24 20:22:23 +00003761UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003762 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003763 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003764}