chain wrap capacity = (large chainring - small chainring) + (large rear cog - small rear cog)
maximum
chain wrap capacity for a RD-m591 is 45 teeth. it's there in plain black-and-white on the linked PDF. so with a 11-40t cassette, the largest chainring your could theoretically use is 16t, if you use a single ring. no one makes a single ring crankset that tiny. so you're outta luck.
(40-11) +
x = 45
29 +
x = 45
x = 45-29
x = 16
If you used a modern 2x10 type crankset with 36 and 24 teeth:
(40-11) + (36-14) =
y
29 - 12 =
y
17 =
y
that's 28t short or 19 chain links short of minimum wrap capacity. you could run a chain with 19 extra links, but when you use the bottom end of your cassette, the chain is going to basically drag on the ground.
if you have, say, chainrings with 42, 32, and 24 teeth, the range 42-24 = 18.
(40-11) + (42-24) =
y
29 - 18 =
y
11 =
y
45-11 = 34, so your a full 34 ring/ cog teeth short, or 17 chain links, short of making that jump. that's a big jump.
yeah, you're going to rip your derailleur off your frame.
since you still have not given us specifics, I can't answer that question any other way but theoretically.
what size rings do you have in the front?
if you limit yourself to the middle and small rings, it might work. still, most middle rings + a 40t cassette is a ridiculously low gear. are you riding with 36" tires? are you, in fact, towing a horse trailer up a mountain? I am not judging, just asking.